I'm new to PHP.
Now I'm trying to fix PHP source other one already have worked.
I succeeded to connect it with mysql using PEAR.
I'm on running it on my local, with MAMP and installed XDebug as well.
Here's the problem.
$select_query = " SELECT SEQ FROM TB_VISITOR WHERE VISITOR_NAME = '$visitor_name' AND VISITOR_PHONE_NO = '$visitor_phone_no' ";
$result =& $db->query($select_query);
if( PEAR::isError($result) )
{
$result_message['result'] = false;
$result_message['error_message'] = 'Search Fail';
}else
{
if( $result->fetchInto($row) )
{
$visitor_seq = $row[0];
}
}
I think that I'm blocked here, and want to know further what the problem was. What I want to see could be expressed in JQuery as 'console.log($result);
so I tried echo, echo $result > Never worked.
Of course, console.log never worked on PHP :'(
and with XDebug I tried like
phpinfo();
I got error message ? on PHP logs
phpinfo() expects parameter 1 to be integer, object given in /Applications/MAMP/htdocs/smartvisiting/visitor_enrollment_proc.php on line 54
so again tried phpinfo($result);
nothing happened.
Please let me know how I can see of 'result' in anyway.
I'm on using Chrome dev mode, Atom as editor tool, and running PHP through MAMP.
$select_query = " SELECT SEQ FROM TB_VISITOR WHERE VISITOR_NAME = '$visitor_name' AND VISITOR_PHONE_NO = '$visitor_phone_no' ";
$result = $db->query($select_query);
if (mysql_errno())
{
echo mysql_error();
}
else
{
if( $result->fetchInto($row) )
{
$visitor_seq = $row[0];
}
}
Related
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.
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
}
We have a website that accesses a SQL Server 2005 server for one query. Currently, the site is in ASP, we are moving it to PHP, and the PHP one is currently being tested. After we run a few successful queries on the PHP site, it returns the "Error in database query. Please try again later" line in the code below. When I rewrote that line with sqlsrv_errors to elaborate, it told me that the table didn't exist. There are about 40 tables in the database, but after the error happens it only shows 8 of them in Management Studio. However, if I allow it to sit for about 5 minutes, all of the tables are restored. No matter how many times the old ASP site is used, the table does not do this. However, when the tables disappear from using the new site, the old site shows inaccessible for a few minutes until the tables re-appear in SQL Server management studio. I didn't see any kind of connection limits on the SQL Server, so I don't know whether it's something I'm doing in the PHP SQL queries or within the SQL Server properties.
<?php
include ("dbvals.inc.php");
if (!empty($_POST['lastnamebox'])) {
$dbhandle = sqlsrv_connect($dbServer, $connectioninfo);
if($dbhandle == false){
echo "Error connecting to database. Please try again later. ";
}
else{
$query = "SELECT * FROM Person WHERE LastName LIKE '%' + ? +
'%' AND InactiveFlag = 'N' ORDER BY LastName, FirstName";
$params = array();
array_push ($params, $_POST['lastnamebox']);
$results = sqlsrv_query($dbhandle, $query, $params);
if($results == false){
echo "Error in database query. Please try again later.";
//This is printed when database tables temporarily disappear
}
else
{
$row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC);
if($row){
do{
echo "<tr><td class='tablecell'>";
echo $row['LastName'] . "," . $row['FirstName'] . "<br>";
echo "Address: " . $row['Address'] . "<br>";
echo "City, State, Zip: " . $row['CSZ'] . "<br>";
echo "</tr></td'>";
}while($row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC));
}
else{
echo "No results found. Please try another query.";
}
}
}
sqlsrv_free_stmt($results);
sqlsrv_close($dbhandle);
}
else {
echo "Please type a value in the search box.";
}
?>
The $query is wrong to begin with. Change it to:
$query = "SELECT * FROM Person WHERE LastName LIKE '%' + ? +
'%' AND InactiveFlag = 'N' ORDER BY LastName, FirstName";
Not sure about the tables disappearing though. What driver are you using?
Just another issue I'm seeing with the code not sure if it's related to your problem. or another copy error but here it is.
if ($r1 = sqlsrv_fetch_array($results)) {
while( $row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC)){
The first fetch should also have '($results,, SQLSRV_FETCH_ASSOC)'
Additional that check is going to eat the first returned record which may or may NOT be what you intended.
Also it is possibly that PHP seeing some results in the while as false even though they aren't and the server is still waiting on you to finish getting the rest. Seen code like that cause 'Server has got away' errors in MySQL which could be what is going on here as well.
Not sure of the syntax but a sqlsrv_clode_cursor() just before the connection close might also fix your issue if there's some kind of connection polling going on. Could be simply running out of connection or getting old one in a incorrect state.
SELECT * FROM Person WHERE AND LastName LIKE
Looks like there something missing between the WHERE and AND to me.
I don't know SQL Server really so it could allow that but it's not standard SQL. Depending on how it's reacting to that error I could see it 'Going away' so to say and reporting tables missing.
I wrote this line of code, but i do not know what happened. I have looked all around the internet for the solution, but none of them seem to fix my issue. I get:
Warning: mysql_query() expects parameter 1 to be string, resource given in /home/mylittle/public_html/style1.php on line 12
yes
When i enter the page. It does not update the style thing in my database. Please help me. I am desperate!
$dbewds = mysql_connect("localhost","mylittle_pony","lol123", "mylittle_pony") or die("Couldn't connect!");
if ($_SESSION['username']) {
$unw = $_SESSION['username'];
$style = 1;
mysql_query($dbewds,"UPDATE `users` SET `style` = '".$style."' WHERE `username` = '".$unw."'");
echo "yes";
} else {
echo "no";
}
?>
$dbewds = mysql_connect("localhost","mylittle_pony","lol123") or die("Couldn't connect!");
mysql_select_db("mylittle_pony");
if (isset($_SESSION['username'])) {
$unw = $_SESSION['username'];
$style = 1;
$query=mysql_query("UPDATE `users` SET `style` = '".$style."' WHERE `username` = '".$unw."'",$dbewds);
if(!$query){
die("query failed".mysql_error());
}
echo "yes";
} else {
echo "no";
}
the connection should be the second variable
I would advise the steps you debug the likely problems next time:
1. try to understand the warning/error message
for example: "mysql_query() expects parameter 1 to be string, resource give", so figure what is a string, what is a resource, according to your code
2. read the manual
go to http://us2.php.net/manual/en/ and search "mysql_query", you can get http://us2.php.net/manual/en/function.mysql-query.php, so figure out how to use the function,
pay attention to the parameters and return, and run the examples under the function intro
3. check your code
btw, mysql_query() will be deprecated as of PHP 5.5.0, MySQLi or PDO_MySQL is better.
Newbie question about PostgreSQL. I'm migrating a MySQL/PHP app I've created, hosted on a Linux server, to PostgreSQL/PHP on a MacOSX Lion Server environment. It's my first experience with Postgres. The first query I'm testing doesn't work as it returns nothing (not even an error message, whichever check code I add). What did I do wrong? I've read articles on the web including the doc on php official website but all comments, personal methods and differences from version to version, either with Postgres or PHP, make it very confusing and I eventually don't understand exactly show I should write my query and fetch_array. Thanks for any suggestions.
Here is my code from the original MySQL application:
// below is the "connexion.php" file
function connexion ()
{
$link=#mysql_connect ("localhost","username","pwd");
if ($link && mysql_select_db ("database"))
return ($link);
return (FALSE);
}
// below is the "index.php" file
require ("connexion.php");
connexion() or exit();
$reqcount = mysql_query ("SELECT * FROM people");
$result = mysql_num_rows($reqcount);
echo "Total : ".$result." people";
mysql_free_result ($reqcount);
mysql_query("set names 'utf8'");
$reqcat = mysql_query ("SELECT catname FROM categories ORDER BY catname");
while ($fieldcat = mysql_fetch_array($reqcat))
{
$name = $fieldcat[catname];
echo $name."<br>";
}
mysql_free_result ($reqcat);
mysql_close ();
And here is the PostgreSQL adaptation:
// connexion.php
function connexion ()
{
$link=pg_connect("host=localhost port=5432 dbname=database user=username password=pwd connect_timeout=5 options='--client_encoding=UTF8'");
return ($link);
}
// index.php
require ("connexion.php");
$reqcount = pg_query ($link,"SELECT * FROM people");
$result = pg_num_rows($reqcount);
echo "Total : ".$result." people";
pg_free_result ($reqcount);
$reqcat = pg_query ($link,"SELECT catname FROM categories ORDER BY catname");
while ($fieldcat = pg_fetch_array($reqcat))
{
$name = $fieldcat[catname];
echo $name."<br>";
}
pg_free_result ($reqcat);
pg_close ();
The php code for postgresql doesn't call connexion() so it never connects, unlike the mysql code.
You can try your query if it fails display error
$reqcount = pg_query ($link,"SELECT catname FROM people") or die(pg_last_error());
This way you can see your error.
errors
UPDATE:
Remove the # in your connection to see the error, and add or die(pg_last_error())
to see the error