Convert MySQL code to MySQLi [closed]

2

3 Errors are presented:

  
  • Notice: Undefined variable: save in C: \ xampp \ htdocs \ index.php on line 6
  •   
  • Warning: mysql_num_rows () expects parameter 1 to be resource, boolean given in C: \ xampp \ htdocs \ index.php on line 133
  •   
  • Notice: Undefined index: page in C: \ xampp \ htdocs \ index.php on line 153
  •   

Here is my code below: index.php

1  <?php
2  include('connection.php'); 
3  include ('paginate.php'); 
4  extract($_POST); 
5  if($save)
6  {
7  $q=mysql_query("select email from users where email='$e'");
8  $r=mysql_num_rows($q);
9  if($r)
10 {
11 echo "O email<font color='red'> ' $e ' </font>j&aacute existe!";
12 }
13 else
14 {
15 
16 $img=$_FILES['img']['name'];
17 
18 $dob=$yy."-".$mm."-".$dd;
19 
20 $query="insert into users values('','$n','$e','$p','$add','$mob','$gen','$img','$hobbies','$c','$dob',now())";
21 mysql_query($query);
22 
23 mkdir("image/$e");
24 move_uploaded_file($_FILES['img']['tmp_name'],"image/$e/".$_FILES['img']['name']);
25 
26 echo "<font color='blue'>Atualizado com sucesso!</font>";
27 }
28 }
29 ?>
130 <?php
131 $per_page = 5; 
132 $result = mysql_query("SELECT * FROM users");
133 $total_results = mysql_num_rows($result);
134 $total_pages = ceil($total_results / $per_page);
135
136 if (isset($_GET['page']))
137 {   
138    $show_page = $_GET['page'];             
139    if ($show_page > 0 && $show_page <= $total_pages) {
140        $start = ($show_page - 1) * $per_page;
141        $end = $start + $per_page;
142    } else {
143
144        $start = 0;              
145        $end = $per_page;
146    }
147 } else {
148
149    $start = 0;
150    $end = $per_page;
151 }
152
153 $page = intval($_GET['page']);
154
155 $tpages=$total_pages;
156 if ($page <= 0)
157    $page = 1;
158 
159 //$query=mysql_query("select * from users");
160 for ($i = $start; $i < $end; $i++)  
161 {
162     if ($i == $total_results)
163      {
164         break;
165      }
166 ?>
167 <tr>
168     <td><?php echo mysql_result($result, $i, 'name');?></td>
169     <td><?php echo mysql_result($result, $i, 'email');?></td>
170     <td><?php echo mysql_result($result, $i, 'address');?></td>
171     <td><?php echo mysql_result($result, $i, 'mobile');?></td>      
172     <td><img src="image/<?php echo mysql_result($result, $i, 'email')."/".mysql_result($result, $i, 'image');?>" width="40px" /></td>
173     <td><?php echo mysql_result($result, $i, 'country');?></td>
174     <td><?php echo mysql_result($result, $i, 'dob');?></td>
175     <td><a href="edit.php?email=<?php echo mysql_result($result, $i, 'email');?>">Edit</a></td>
176     <td><a href="delete.php?email=<?php echo mysql_result($result, $i, 'email');?>&image=<?php echo mysql_result($result, $i, 'image');?>">Delete</a></td>
177 </tr>  
178 
179 <?php }  ?>
180 <tr>
181 <?php 
182  $reload = "index.php" . "?tpages=" . $tpages;   
183  if ($total_pages > 1) {
184      echo paginate($reload, $show_page, $total_pages);
185      }
186 ?>
187 </tr>
188 </table>

connection.php

<?php 
$mysqli = new mysqli('localhost','root','','bd') or die("ERROR CONECTION!");
?>
    
asked by anonymous 24.10.2015 / 17:57

1 answer

3

I prefer to use

$conn = mysqli_connect('localhost','root','','bd');

mysqli_connect is an alias for the class constructor, you can test the connection by checking $ conn

if(!$conn){
 echo mysqli_connect_error();
}

Now you can query and manipulate the database, but you need to use the variable $ conn

$result = mysqli_query($conn, "SELECT * FROM users");

while($user = mysqli_fetch_assoc($result)) {
 echo $user['nome'];
}

About the errors:
1 The error says that on line 6 you are doing if on a variable that was not defined previously;
2 Consequence of not having a connection the function mysql_query () does not return the value that mysql_num_rows () needs;
3 It says that the index page in $ _GET ['page'] does not exist, values in $ _GET are passed via URL ( link ) or input form with the attribute method = 'get'; Home Whenever you start a technology start with RTFM: D link

    
24.10.2015 / 19:43