Convert Mysql to Mysqli

Asked

Viewed 254 times

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!");
?>
  • In this case came even handy these line numbers, to help point out what is wrong or what should be modified by mentioning the line number for modification, greatly facilitating the work for responders. I think a feature like this would be very useful in code review. + 1!

  • 1

    +1 @Chun for sure!

1 answer

3

I prefer the use of

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

mysqli_connect is an alias for the class builder, you can test the connection by checking $Conn

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

now you can query and manipulate the bank, but it is necessary to use the $Conn variable

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

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

About the mistakes:
1 Error says that on line 6 you are doing if on a variable that was not previously defined;
2 Consequence of not having a connection the function mysql_query() does not return the value that mysql_num_rows() needs;
3 Says that the index page in $_GET['page'] does not exist, values in $_GET are passed via URL (http://localhost? page=1) or input form with method='get attribute';

Whenever you start a technology start with RTFM :D http://php.net/manual/en/book.mysqli.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.