I've been told that I was using an deprecated version of PHP, mysql_query, instead of the newer version mysqli_query, and soon it will be remove. Knowing that, I quickly tried to update all of my old, deprecated codes into the newer code. Doing that, I quickly ran into a problem, but I'm not sure what I'm doing wrong. Please take a look:
<?php
$connect = mysqli_connect('server','username','password','database');
$fetch = mysqli_query($connect,"SELECT username FROM userLogin");
while($row=mysqli_fetch_array($fetch,MYSQLI_NUM)){
//do something
}
?>
output:
Warning: mysqli_fetch_array() expects parameter 2 to be long, string given in /home/a2056400/public_html/test4.php on line 8
I have a feeling the error message has something to do with the conditional statement inside the while loop, but I'm not sure what is wrong with it. Please help in any way. Thank you.
You are not connecting to the database properly.
$connect = mysqli_connect('server','username','password','database');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$fetch = mysqli_query($connect,"SELECT username FROM userLogin");
while($row=mysqli_fetch_array($fetch,MYSQLI_NUM)) {
//do something
}
Related
I have a problem and I hope someone can help me solve it.
I had to upgrade my VPS and from CentOS 6.x I switched to CentOS 7.5
So now I find PHP 7.0.1 and MySQL 5.7
The script I write below, worked great on
PHP 5.6 and MariaDB and now goes wrong.
What can be a valid solution for this script to work properly?
Thank you all.
$connessione = mysql_connect("localhost", "myuser", "mypassword");
mysql_select_db("mydb", $connessione);
$risultato = mysql_query("SELECT * FROM mytable", $connessione);
$num_righe = mysql_num_rows($risultato);
if ($num_righe == 0) {
echo "There are no new products";
} else {
$query ="SELECT * FROM mytable";
$nuovi_prodotti= mysql_query($query, $connessione) or die(mysql_error());
$row_nuovi_prodotti = mysql_fetch_assoc($nuovi_prodotti);
$intes ="Riferimento|ID|nome|plain_description|iva inclusa|id_fornitore|brand|tax|picture1|picture2|picture3|model_size|model_quantity|barcode";
$fornitore ="1";
$iva = "22";
$righe.="".$intes."\n";
do {
$righe.= "".$row_nuovi_prodotti['reference']."|".$row_nuovi_prodotti['id_categoria']."|".$row_nuovi_prodotti['descrizione']."|".$row_nuovi_prodotti['descrizione']."|".$row_nuovi_prodotti['prezzoacquisto']."|".$row_nuovi_prodotti['prezzovendita']."|".$fornitore."|".$row_nuovi_prodotti['marca']."|".$row_nuovi_prodotti['dispo']."|".$row_nuovi_prodotti['EAN']."|".$iva."|https://www.mysite.it/cat/".$row_nuovi_prodotti['reference'].".jpg\n";
}
while ($row_nuovi_prodotti_unica= mysql_fetch_assoc($nuovi_prodotti_unica));
$filename = "nuovi-prodotti-tagliaunica.csv";
file_put_contents($filename, $righe);
echo "New products found: $num_righe - file CSV ok \n";
}
mysql_close($connessione);
The problem occurs because the mysql_* syntax is deprecated in PHP 7.0 and forward. You will need to change to mysqli_* or PDO.
You mention in the comments that you "already opted for mysqli", but the syntax provided in your question is a mixed syntax of mysql and mysqli. mysqli takes the database connection as a parameter, while mysql does not.
Therefore, change your mysql_* to mysqli_*.
Examples from your question:
mysql_query($query, $connessione)
to
mysqli_query($query, $connessione)
etc.
If you're in doubt about what the syntax is for the different mysqli_* functions, then I would suggest looking it up. This thread is a good place to start if you're changing from mysql to mysqli.
This question already has an answer here:
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
(1 answer)
Closed 8 years ago.
My host recently upgrade the PHP version and a certain part of my website now shows the following error:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in url/structure/here on line 49
That is referencing the below code:
function DBConnect() {
$this->connectCount ++;
//echo "$this->connectCount<br>";
if ($this->dbType == 'mysql') {
$dbConnect = mysql_connect($this->dbHost, $this->dbUser, $this->dbPasswd) or die ("MySql Connection Failed: " . mysql_error());
mysql_select_db($this->dbName, $dbConnect);
}
if ($this->dbType == 'postgresql') {
$dbConnect = pg_connect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
//$dbConnect = pg_pconnect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
}
return $dbConnect;
}
I'm aware the fact that this is because the current way my site connects to MYSQL is now outdated in the new version of PHP but does anyone know how I would update the above code to make this work?
The easier way is to use mysqli_connect(). The syntax is very similar to what you would had with mysql_connect(),which means the changes in your code will be minor and easy to make.
Pdo would be the safest, but if you are trying to get you site back on quickly, the mysqli_* commands will achieve that.
Google (or check on stackoverflow) mysql vs mysqli. You ll find plenty of examples.
Hope this helps.
Good luck
--
Sorry after re-reading i see you asked what needs to be change on your source code. Afraid i cannot help right now as i am responding from a mobile phone :(
This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 9 years ago.
Ok, so I know the question about 'why am I getting this warning with mysql_fetch_array...' has been asked several times, my problem is all the accepted answers state that the reasons the server is spitting this warning out is because the query itself is incorrect...this is not the case with me.
Here is the code below:
$dbhost = "host";
$dbuser = "user";
$dbpass = "pass";
$dbname= "db";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die ('<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Failed.php?dberror=1">');
$token = mysql_escape_string($_GET['token']);
$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
do stuff...
}
Everything within the 'while' statement is being executed just fine - it makes some changes to the DB which I can validate is happening. More importantly, the query never spits out any error details. I've tried testing for cases where $result===false and asking for error info but it won't return anything then either. From what I can tell, the query string is fine and is not failing.
What am I doing wrong here? Could there any other reason why PHP doesn't like my While parameters other than the SQL statement is bad (which again, I'm convinced it's not bad)?
Also, I know I should be using mysqli/PDO....I plan to switch over to that in the near future, but I'm pulling my hair out just trying to make this work and I have no idea why it won't. It's more of a personal thing at this point...
Thanks for your help, and let me know if you need any additional info. (PHP Version 5.3)
Here is an echo of the query string ($query):
SELECT * FROM newuser WHERE token='6e194f2db1223015f404e92d35265a1b'
And here is a var_dump of the query results ($result): resource(3) of type (mysql result)
$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
do stuff...
}
If the die statement is not executed, $result is OK when you enter the while loop. The problem then is probably that you use $result for a query inside the loop as well, eventually leading to it being set to false.
So for now i can say that the problem is not the mysql_escape_string nether the using of mysql at all neither access privilege from user name and password and what i want to tell you is to test the $result and if it is a resource proceed with your while block like this
if(is_resource($result))
{
while($row = mysql_fetch_array($result))
{//process your code here}
}
and tell me if the code has been also executed :)
The query is not correct according to mysql_. The error you're receiving is telling you that $result is boolean (false).
Where is $token coming from? You best stop using mysql_ functions and use a prepared statement and a bound parameter.
your escape is wrong try this
$token = mysql_real_escape_string($_GET['token']);
instead of $token = mysql_escape_string($_GET['token']);
This extension is deprecated as of PHP 5.5.0, and will be removed in the future.
http://php.net/manual/en/function.mysql-real-escape-string.php
This question already has answers here:
what is the alternate function mysql_list_tables() in php 5
(5 answers)
Closed 9 years ago.
I'm trying to add a voting system for my website, and I get this error:
Deprecated: Function mysql_list_tables() is deprecated in /home/yokoscap/public_html/vote/classes/vote.class.php on line 11
Access denied for user 'yokoscap'#'localhost' (using password: NO)
I'm new to PHP and MySQL, so if you could dumb it down/help me fix it it'd be great.
Here's the code paste, I thought it was too big to go in here so I uploaded to pastebin.
Did you check the manual? There is a complete example on a replacement for it:
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
use this sql query
show tables from dbname
hope this will help
The mysql_list_tables() function has been deprecated since PHP 4.3.7 (!) because it's entirely too specific.
To get a list of tables in the current database, run a SHOW TABLES query. It's a perfectly normal query; you don't need a special function just to do that.
I says here in the manual
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
According to mysql_list_tables ...changelog secton
This function became deprecated from version 4.3.7
Use SHOW TABLES
Can anyone see why the code below is not working? Is it because of the deprecated sql syntax? I'm stumped with this one.
<?php
mysql_connect("localhost",$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$usersearch=$_POST['search'];
$query="SELECT * FROM tracks WHERE 'artist' LIKE '%$usersearch%'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row["artist"]." | ".$row["name"];
echo "<br>";
}
?>
First, do not use mysql_* please. This extension is deprecated as of PHP 5.5.0.
http://php.net/manual/en/function.mysql-query.php
You can use mysqli_query() or PDO::query().
Probably beacuse you have single quotes(') and not ` when selecting your table. And as pointed out don't use mysql_* use PDO or mysqli, even if its just a school project(old habits die hard). Also you could add
mysql_query($query) or die($mysql_error());
This will probably point out whats wrong..
and for last, please escape dynamic input variables
add to the top of page:
error_reporting(E_ALL);
ini_set("display_errors", 1);
and open the page in an browser. are there any errors?
change (this mutes errors)
#mysql_select_db($database) or die( "Unable to select database");
to :
mysql_select_db($database) or die( "Unable to select database");
A word of advice though, better to use the MySQLi or [PDO_MySQL][3] instead of mysql_connectsince it will soon be deprecated:
Warning
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_connect()
you can find more info of how to use them on this answer.
UPDATE
check if you get an empty result, add :
$num=mysql_numrows($result);
if ($num === 0) echo "Empty result"
and open the page again. if the page is still blank, change:
echo $row["artist"]." | ".$row["name"];
echo "<br>";
to:
vardump($row);
UPDATE 2
If you did get Empty result, than add
echo $query;
and try to open the page and run the query manually