Moving from php5.2.17 to 5.3.3 - php

I am moving a server from Centos 5 php5.2.17 to Centos 6 php5.3.3
I am facing up a problem with a code I didn’t wrote. There’s no problem in the old server for around two years but in the new one, tests warn me:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in path_to_template/template.php on line 43
Searching recursively:
template.php ->line 43:
while($value_comment = mysql_fetch_array($result_comments))
$result_comments = getComments($unique_id);
function getComments($unique_id)
{
$query = "SELECT * FROM (...)";
$result = mysql_query($query);
return $result;
}
$link = mysql_connect($host,$username,$password);
$db = mysql_select_db($db,$link);`
$host “…”;
$username = “…”;
$password = “…”;
$db = “…”;`
To get an output error I changed $result: $result = mysql_query($query) or die(mysql_error());
No database selected
I don’t realize what has changed between both php versions
Found the solution
mysql username was imported but without privileges on any database (automatic things...!)

maybe your old server php error reporting is switched off.
You recive this error, when the mysql returns 0 records.
$result = mysql_query($query);
$numResults = mysql_num_rows($result);
if ($numResults > 0) {
// mysql_fetch_array
}

Related

PhP script not running in server even though it worked in my laptop with same parameters

Hi there again community! hope you are doing well!
I have created a code in my laptop using php ver 7.2 in WAMP server.
The code consist in 2 php files. One has html/php and the other has php/sql.
When I executed the code in my laptop, it runned without any problems.
When I do the same in my live server, it gives out a run problem. I have tried to solve this but can't seem to find out what it is. It's probably something abvious but since I am new to php and servers, I can't seem to pinpoint the problem.
Thank you all for your continous help as always!
For privacy reasons I can't give out my html/php code. However I did create a very simple php/sql code that throws the exact same problem.
Here is the php/sql simple code that gives the exact same problem:
<?php
//Gets server connection credentials stored in tempCredentialsFoile.php
require_once('tempCredentialsFoile.php');
$command = "SET AUTOCOMMIT = 0";
$result = mysqli_query($con, $command);
$command = "BEGIN";
$result = mysqli_query($con, $command);
$Name_Selected = $_POST['name_selected'] ?? '0';
$sql = "INSERT INTO table (name) VALUES ('$Name_Selected')";
$result = mysqli_query($con, $sql); //Executes query.
// The "??" after the POST method is a new function in php v7.2.
// If by anychance the POST method doesnt receive any input, it automatically
// assigns the value that is between the commas. This being the number '0' in my case
if($result){
$command = "COMMIT";
$result = mysqli_query($con, $command);
echo "<br>Tables have been saved witn 0 errors.";
} else {
$command = "ROLLBACK";
$result = mysqli_query($con, $command);
echo "<br>Error! Databases could not be saved. <br>";
}
$command = "SET AUTOCOMMIT = 1"; //return to autocommit
$result = mysqli_query($con, $command);
//Close the sql connection to dababase
mysqli_close($con);
?>
PS: I am using PHP v7.2
EDIT: Here is the image of the error.
I found the problem.
Aparently the php version of the server I have at work doesn't accept the new php 7.1 new funtions since it's still in v5.
I am refering to the "?" after the POST methods brackets.
$Name_Selected = $_POST['name_selected'] ?? '0';
The "??" after the "]" means that if the post doesnt have a declared value in it when executing, you are going to automatically declare it with the value you have placed in quotation after the "??". In my case it the character 0.
$Name_Selected = $_POST['name_selected'] ?? '0'; <--
After eliminating the "??" from the code, it all worked property.

Convert data from db to JSON using php

I have already seen many questions but nothing has helped.
I want to convert my data from database (MySQL) to JSON using PHP. This is my PHP code:
init.php
<?php
$db_name = "webappdb";
$mysql_user = "root";
$mysql_pass = "root";
$server_name = "localhost";
$charset= "utf8";
$con = mysqli_connect($charset, $server_name, $mysql_user, $mysql_pass, $db_name);
?>
listViewBooks.php
<?php
include("init.php");
header('Content-Type: application/json');
// get all items from user_info_book table
$sql = mysqli_query("SELECT * FROM `user_info_book`");
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
$output[] = $row;
}
echo json_encode($output);
echo json_last_error();
mysqli_close($con);
?>
The error is 0, so it's nothing.
There are a bunch of problems in your code. For starters, you have this:
$sql = mysqli_query("SELECT * FROM `user_info_book`");
$res = mysqli_query($con,$sql);
$sql is a mysqli_result object on success or boolean false on failure. Here, it's false because you didn't pass the database link ($con). See the docs. You shouldn't, don't need to, and can't store the result of mysqli_query in a variable ($sql) and then pass that variable in another call to mysqli_query. Just do:
$sql = "SELECT * FROM `user_info_book`";
$res = mysqli_query($con, $sql);
Also, you initialize one array, then add to another:
$result = array();
while($row = mysqli_fetch_array($res)){
$output[] = $row;
}
Perhaps you mean to do $output = array();?
You would benefit from using an IDE like PHPStorm.
So you execute a query and assign the result to $sql:
$sql = mysqli_query("SELECT * FROM `user_info_book`");
But then you query again and use the $sql result as if it were a string:
$res = mysqli_query($con,$sql);
Probably more what you were thinking:
$sql = "SELECT * FROM `user_info_book`";
$res = mysqli_query($con,$sql);
You should use error reporting when developing:
error_reporting(E_ALL);
ini_set('display_errors', '1');
You code is bad:
$sql = mysqli_query("SELECT * FROM `user_info_book`");
^---missing DB handle
^---query result handle
So $sql becomes boolean false for failure, because you didn't call the query function correctly.
You then blindly use this boolean false as if it was a query string:
$res = mysqli_query($con,$sql);
^---boolean false, due to previous errors
So basically, you assumed your code was perfect, and could never ever possibly have any problems, so failed to add any error handling. Since you have no error handling, you utterly ignored the errors that DID occur.
Never EVER assume success - this code is a perfect example of WHY. Your sql is syntactically perfect, yet it failed because you didn't call the query function properly.
Always assume failure, check for failure, and treat success as a pleasant surprise.
You initialize an array called $result but try to use an array with the $output identifier which has not been initialized, PHP will not auto-initialize an array variable for you. That is where one error comes from.
The second error I noticed is:
mysqli_query("SQL")
You forgot to pass in the database connection resource as it should be:
mysqli(db_connection, sql_query)
Fix those then if you need more assistance, comment below.
Have a good day.

Connection is made to the database with php script but no values are returned

I have a successful connection to the database through this php script but it is not returning any values even though it is connected. I am checking for the results on my web browser and it just returns a blank screen. I have used the same script (different queries) to access two other tables in the database and they are both working fine. Here is my code:
<?php
$username = "xx";
$password = "xxx";
$host = "xxxxx";
$database="xxxxx";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$myquery = "SELECT `AUTHOR`, `In_order` from `authors`";
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
It is probably some silly mistake that I have over looked but I have been stuck on it for longer than I should! thanks in advance for any feedback
Tried you code locally on some data and it returns everything ok.
I needed to change the select to match my data
So I am 95% sure the problem is in your query / db settings.
I would first check if your columns in database is really called AUTHOR and 'In_order' with the exact capital letters.
MySql names can be case sensitive depending on your db server settings, and this could be the problem
Sidenote: if you can research mysqli and pdo for connecting to DB instead of mysql that is deprecated.
Try this:
$myquery = "SELECT `AUTHOR`, `In_order` from `authors`";
$query = mysql_query($myquery);
$num = mysql_num_rows($query);
var_dump($query);
var_dump($num);
echo mysql_error();
and tell us what it all says.
Edit: okay, so it's 231 rows in your table, as the var_dump($num) says. Now let's try and get them at last, but in a slightly more efficient way:
while ($row = mysql_fetch_assoc($query)) {
$data[] = $row;
}
echo json_encode($data);
I have a feeling that your "for" loop and mysql_fetch_assoc() inside is what plays tricks with you, because both of them use different internal counters.

mysqli_error() expects parameter 1 to be mysqli, null given

I have a a form that pulls data from a database(mysql to be specific) and echos the data into the value section of <input> tags. It doesn't seem to be working I have coded a view section of my website to do the same thing but from a different table in my database. I use the same code to make making changes easy and if another developer works on my site in the future. Anyway it doesn't seem to be working I'm not sure why though.
The full error I get:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/caseol5/public_html/jj/admin/news_update.php on line 9
Here is line 9 that the error is referring to:
$result = mysqli_query($link,$sql);
I know that both of those function are not null as I did:
echo $link
echo $sql
before that line after I started feting the error and they both are not null.
Here is the full code segment:
$nid = $_GET['nid'];
include ("../sql/dbConnect.php");
$sql = "SELECT * FROM jj_news WHERE news_id = $nid";
echo "<p>The SQL Command: $sql </p>";
echo "<p>Link: $link </p>";
$result = mysqli_query($link,$sql);
if (!$result)
{
echo "<h1>You have encountered a problem with the update.</h1>";
die( "<h2>" . mysqli_error($link) . "</h2>") ;
}
$row = mysqli_fetch_array($result);
$ntitle = $row['news_title'];
$ntline = $row['news_titleline'];
$ndesc = $row['news_desc'];
$nother = $row['news_other'];
I have looked into mysqli_query and I can't find anything I'm missing. I have also tired breaking the code down (and running parts of it and it gives the same error. My guess is it something small that I missed. I've looked at other question on this site that do that are a little similar but none seem to help. I've been looking at this for a while now and need another pair of eyes.
Update
As requested the contents of my dbconnect.php file:
$hostname = "localhost";
$username = "caseol5_jjoes";
$database = "caseol5_jj_site";
$password = "password1";
$link = mysqli_connect($hostname, $username, $password, $database);
$link = mysqli_connect($hostname,$username,$password,$database) or die("Error " . mysqli_error($link));
if (!$link)
{
echo "We have a problem!";
}
As clearly stated in the error message, mysqli_querydocs expects the first parameter to be a mysqli resource. In your case, this parameter is called $link but it holds a null value. A proper mysqli resource is normally obtained from connecting with the database by making use of mysqli_connectdocs
I expect the ../sql/dbConnect.php file holds the logic to connect with the database. Verify whether the $link variable is indeed initialized there. If it's not there, try to find an occurrence of mysqli_connect - maybe the resource is set to a different variable.
Without knowing what exactly is in ../sql/dbConnect.php, your problem right now is that you do not have a valid mysqli resource to use for mysqli_query.

PDO code not working

i am new to PDO.. i tried out some online tutorials and found some step-by-step guides. i am using WAMP, i created a database named "try" with table named "books".
Now in my index.php i wrote:
<?php
$host = "localhost:3306";
$db = "try";
$user = "clyde";
$pass = "moonfang";
$conn = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
$sql = "SELECT * FROM books";
$q = $conn->query($sql) or die("failed!");
while($r = $q->fetch(PDO::FETCH_ASSOC)){
echo $r['title'];
}
?>
Now whenever i load localhost on my browser i see these errors;
i dont understand the problem.. :-(
According to several examples it seems that PDO prefers the host and the port in the dsn for itself:
$host = "localhost";
$port = 3307;
$conn = new PDO("mysql:host=$host;port=$port;dbname=$db",$user,$pass);
Here's the PHP manual for the PDO MySQL DSN. Note the "port" part.
The problem is that the target machine actively refused [the connection].
Therefore you have to check if usernames/passwords/access-rights to the database server are all OK; including IP/host and port/firewall settings.
Line 8 where error occurs is $q = $conn->query($sql) or die("failed!"); This line mixes mysql_ and PDO and is not required.
The code should look like
$sql = "SELECT * FROM books";
while($r = $sql->fetch(PDO::FETCH_ASSOC)){
echo $r['title'];
}
You should also incorporate PDO error handling
`

Categories