Hack Forums
[PHP] mysql_fetch_array to mysqli - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] mysql_fetch_array to mysqli (/showthread.php?tid=5729794)



mysql_fetch_array to mysqli - deadsh0t - Oct 3, 2017

Im trying to update my code to mysqli to make it php 7 ready so i updated the db connection and now it works fine however executing and getting result from a query doesnt give any response anyone knows what im doing wrong ?
here the part
PHP Code

<?php 
$sql
="SELECT * FROM tbl_uploads";
        
$result_set=mysqli_query($sql);
        while(
$row=mysqli_fetch_array($result_set)) 



* - Lrrr - Oct 3, 2017

(Oct 3, 2017 06:48 AM)deadsh0t Wrote:  Im trying to update my code to mysqli to make it php 7 ready so i updated the db connection and now it works fine however executing and getting result from a query doesnt give any response anyone knows what im doing wrong ?
here the part
PHP Code

<?php 
$sql
="SELECT * FROM tbl_uploads";
        
$result_set=mysqli_query($sql);
        while(
$row=mysqli_fetch_array($result_set)) 

if you run the following code:
PHP Code

<?php 
$sql
="SELECT * FROM tbl_uploads";
        
$res = mysqli_query($sql);
        
var_dump(mysqli_fetch_array($result_set)); 

And you get a NULL response, you should run the same query locally on the DB to insure it actually returns data.


* - deadsh0t - Oct 3, 2017

(Oct 3, 2017 09:24 AM)Lrrr Wrote:  if you run the following code:
PHP Code

<?php 
$sql
="SELECT * FROM tbl_uploads";
        
$res = mysqli_query($sql);
        
var_dump(mysqli_fetch_array($result_set)); 

And you get a NULL response, you should run the same query locally on the DB to insure it actually returns data.

If i change it back to mysql instead of mysqli it works fine the query returns data it inly turns to no result when trying to convert it to mysqli


* - remix.cpp - Oct 3, 2017

(Oct 3, 2017 09:35 AM)deadsh0t Wrote:  If i change it back to mysql instead of mysqli it works fine the query returns data it inly turns to no result when trying to convert it to mysqli

because u need to pass a mysqli instance to mysqli_query

example:

PHP Code

<?php 
$mysqli 
= mysqli_connect("localhost", "my_user", "my_password", "world");
$sql="SELECT * FROM tbl_uploads";
$result_set=mysqli_query($mysqli, $sql);
while(
$row=mysqli_fetch_array($result_set)) 



* - Squito - Oct 3, 2017

Also either use mysqli_fetch_assoc instead of mysqli_fetch_array or use the second parameter of mysqli_fetch_array.


* - deadsh0t - Oct 3, 2017

(Oct 3, 2017 10:43 AM)remix.cpp Wrote:  because u need to pass a mysqli instance to mysqli_query

example:

PHP Code

<?php 
$mysqli 
= mysqli_connect("localhost", "my_user", "my_password", "world");
$sql="SELECT * FROM tbl_uploads";
$result_set=mysqli_query($mysqli, $sql);
while(
$row=mysqli_fetch_array($result_set)) 

Yeah this worked thanks however i include a dbconfig.php at the beginning at the file can you explain why i needed t open a new connection instead of using the connection already present ?
Or is this specific for mysqli ?


* - Squito - Oct 3, 2017

(Oct 3, 2017 01:02 PM)deadsh0t Wrote:  Yeah this worked thanks however i include a dbconfig.php at the beginning at the file can you explain why i needed t open a new connection instead of using the connection already present ?
Or is this specific for mysqli ?

http://php.net/manual/en/mysqli.query.php
Quote:link
Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()




* - deadsh0t - Oct 3, 2017

(Oct 3, 2017 01:12 PM)Squito Wrote:  http://php.net/manual/en/mysqli.query.php

Thanks! this explained it pretty well


* - mobi35 - Oct 7, 2017

(Oct 3, 2017 06:48 AM)deadsh0t Wrote:  Im trying to update my code to mysqli to make it php 7 ready so i updated the db connection and now it works fine however executing and getting result from a query doesnt give any response anyone knows what im doing wrong ?
here the part
PHP Code

<?php 
$sql
="SELECT * FROM tbl_uploads";
        
$result_set=mysqli_query($sql);
        while(
$row=mysqli_fetch_array($result_set)) 

PHP Code

<?php 
$connection 
= mysqli_connect("host","user","pass","dbname");

$sql="SELECT * FROM tbl_uploads";
        
$result_set=mysqli_query($connection,$sql);
        while(
$row=mysqli_fetch_array($result_set)) 

I think you need to include the connection on the query


* - Nyctophiliac - Oct 13, 2017

(Oct 3, 2017 06:48 AM)deadsh0t Wrote:  Im trying to update my code to mysqli to make it php 7 ready so i updated the db connection and now it works fine however executing and getting result from a query doesnt give any response anyone knows what im doing wrong ?
here the part
PHP Code

<?php 
$sql
="SELECT * FROM tbl_uploads";
        
$result_set=mysqli_query($sql);
        while(
$row=mysqli_fetch_array($result_set)) 

mysqli_query requires 2 parameters

1.) Mysqli Database link
2.) Query

You're giving it only the query.