I have this same query running on other PHP scripts, but it now won't work. I have troubleshooted for eight hours and frustrated like no one knows.
<?php
$con = mysqli_connect("localhost","dave1_guest","password1")
or die('Could not connect: ' . mysqli_error());
mysqli_select_db("dave1_main",$con) or die(mysqli_error());
$result = mysqli_query("SELECT * FROM Inventory limit 100");
while($row = mysqli_fetch_array($result)) {
echo $row['ItemNumber'];
}
mysqli_close($con);
?>
I tried running the query on PhpMyAdmin, and it returns 0 results! This is impossible. When I open the table with PhpMyAdmin, it opens and shows thousands of rows!
When I run the query that PhpMyAdmin uses, it works. But when I add a constraint like WHERE StockNumber='13922', it does not.
Again, the stupid thing is that this script works on other PHP pages on my site.
You are fetching the data twice. Remove this line $row = mysqli_fetch_array($result);
I do not know what your problem, so I do not see the exact error, but I'll try a little help.
First of all:
$con = mysqli_connect("localhost","dave1_guest","password1") or die('Could not connect: ' . mysql_error());
mysqli_select_db("dave1_main",$con) or die(mysql_error());
$result = mysqli_query("SELECT * FROM Inventory LIMIT 100");
$row = mysqli_fetch_assoc($result);
print_r($row);
while($row)
{
echo $row['ItemNumber'];
}
mysqli_close($con);
Try this!
Related
Ive been trying to display a "bid" from the database to no success.
here is my error
Fatal error: Function name must be a string in /home/rslistc1/public_html/get-bids.php on line 7
here is my code
<?php
include('session.php');
?>
<?php
require_once('mysql_connect.php');
$query3 = "SELECT id, username, bid FROM bids WHERE username = '$login_session'";
$result3 = mysql_query($query3) OR die($mysql_error());
$num = mysql_num_rows($result3);
while ($row = mysql_fetch_array($result3, MYSQL_ASSOC)) { ?>
<?php echo''.$row['bid'].'';
}
?>
Any idea
Before we address the line 7 issue, lets check other errors. In order to request a query to a MYSQL database, we need to create a connection:
$con = mysqli_connect("ip_address","user","password","database_name");
Once we have that connection, let us check if we can actually connect to the database:
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
Appreciate that mysqli_error() function uses the connection. Now the query string:
$query3 = "SELECT id, username, bid FROM bids WHERE username = '$login_session'";
You are sending a query to look for a username called "$login_session" and it would most likely not find any match. To add strings from variables will be as follow:
$query3 = "SELECT id, username, bid FROM bids WHERE username = '" . $login_session . "'";
Now, for the error in line 7
result3 = mysql_query($con, $query3) OR die($mysql_error($con));
As you can see, both mysql function use the connection to check for errors. Try it and let me know if everything works fine.
Edit:
Terribly sorry my friend, I just forgot to put a little letter "i" on the line, also, I would like to show you my way to deal with the query result. First, the line as it should be:
$result3 = mysqli_query($con, $query3);
Notice the i after mysql. Now let us check whether we got some rows or not:
if (!$result3) {
die('Could not retrieve data: ' . mysqli_error($con));
} else {
while ($row = mysqli_fetch_array($result3)) {
//Show your results
}
}
Can anyone tell me a reason a query just wont return the same data in php as the ones that it returns on phpmyadmin sql?
$query = "UPDATE `boards` SET `contestPlaces`=0, `contestPlacesFilled`=0";
$result = mysql_query($query) or die("ERROR:QUERY_FAILED timeset 8" . mysql_error());
$query = "UPDATE `playerspoints` SET `points`=0";
$result = mysql_query($query) or die("ERROR:QUERY_FAILED timeset 9" . mysql_error());
$query = "SELECT `avatarId`, `points` FROM `contestants`";
$result = mysql_query($query) or die("ERROR:QUERY_FAILED timeset 10" . mysql_error());
$qualified = array();
while($row = mysql_fetch_row($result));
{
print_r($row);
$qualified[] = $row;
} `
Result: Array ( [0] => ) SUCCESS.
I get no error, it just returns an empty result, while in phpmyadmin sql tab, it runs fine.
I'm properly connected with the db, cause I run queries before this one. I checked to see and this one is the only one that fails without an apparent reason. So What should I look in order to see what's going wrong?
The user that I get connected to the database has the following privileges:
SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, CREATE VIEW, EVENT, TRIGGER, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EXECUTE
remove the semicolon:
while($row = mysql_fetch_row($result));
^
//this is latest code in php5.7.14 and accesses the database.....but still it was not working.
//therefore, i found that the database coallition was not 'utf_general_ci' rather i kept default leading to 'latin_swedish'.
//single mistake can ruin your project.
<?php
$mysqli_hostname = "localhost";
$mysqli_user = "root";
$mysqli_password = "krunal";
$mysqli_database = "krunal";
$bd = mysqli_connect($mysqli_hostname, $mysqli_user, $mysqli_password,$mysqli_database);
if(mysqli_connect_errno()){die("database connection failed");}
?>
<?php
$sql= "SELECT * FROM `done`;";
$result=mysqli_query($bd,$sql);
if(!$result){
die("database query failed". mysqli_connect_error()." (".mysqli_connect_errno().")");}?>
<?php while($row=mysqli_fetch_row($result)){
var_dump($row); }?>
<?php mysqli_close($bd);?>
I am using php to get records from a mysql database using the following code:
<?php
$username="";
$password="";
$database="";
$hostname="";
$con = mysql_connect($hostname, $username, $password);
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $con);
if(isset($_POST['emp'])){
$emp = $_POST['emp'];
$result = mysql_query("SELECT * FROM contact_log", $con);
echo mysql_num_rows($result);
die();
while($row = mysql_fetch_array($result)){
$emp = $row['emp'];
echo $emp.'<br>';
}
die();
}
mysql_close($con);
?>
This works fine and returns the correct fields. The problem is that if I change the query to
$result = mysql_query("SELECT DISTINCT * FROM contact_log", $con);
or
$result = mysql_query("SELECT * FROM contact_log GROUP BY emp", $con);
no results are returned.
mysql_num_rows does not even return a value which indicates to me that those lines are breaking my code but I am unable to figure out how.
I doubt you want to do a distinct * on your first query. Looking at your code, you probably want:
"SELECT DISTINCT emp FROM contact_log"
And you can get more information about what is going wrong with mysql_error:
mysql_query("select * from table") or die(mysql_error())
Finally, are you sure that $_POST['emp'] is being sent? Put an echo right after that if to make sure. And just so you know, you aren't using the emp POST variable for anything other than a flag to enter that block of code. $emp = $_POST['emp']; is doing absolutely nothing.
I am having a problem with a MySQL query. When ever I run this query it takes over 10 seconds to return the rows. However if I change my limit to 29 it returns it in less than 1 second. My Question is my implementation and query in good shape, or am I making a mistake here that would cause this issue?
<?
try
{
$con = mysql_connect("XXX.XXX.XXX.XXX","XXX","XXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bis_co", $con);
$query = "SELECT Name, Value FROM `bis_co`.`departments` LIMIT 31";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
while($row = mysql_fetch_assoc($result)){
echo $row['Name'] . "<br />";
}
mysql_close($con);
}
catch(Exception $e)
{
echo $e;
}
?>
You could try getting rid of the
$row = mysql_fetch_array($result)
statement. The if statement will take care of returning the array. You could also change the query string to
"SELECT Name, Value FROM `departments` LIMIT 31"
since you're already setting the database name in the mysql_select_db statement. Though, none of these should be causing your issues. It seems like it's a table issue. Have you tried it with mysqli and prepared statements?
I am very new to PHP and can't seem to use mySQL data at all from PHP. The SQL query I wrote works fine in phpMyAdmin when I run it in the SQL editor, but the most I am able to get from the code is the following from var_dump. Nothing else displays anything at all.
resource(3) of type (mysql result)
Any help at all would be greatly appreciated!!
<?php
ini_set(‘display_errors’,1);
error_reporting(E_ALL|E_STRICT);
$con = mysql_connect("localhost","XXXXXXXX","XXXXXXX");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sharetrader", $con);
$sharelist = mysql_query("SELECT DISTINCT tblStocks.stockSymbol, tblShareData.lookupDate FROM tblStocks LEFT JOIN tblShareData ON tblShareData.tickerCode = tblStocks.stockSymbol ORDER BY tblShareData.lookupDate ASC LIMIT 0 , 30");
if (!$sharelist) {
die('Invalid query: ' . mysql_error());
}
var_dump($sharelist);
while ($row = mysql_fetch_array($sharelist)) {
echo $row['tblStocks.stockSymbol'];
}
mysql_close($con);
?>
I am very new to PHP
But you're making great progress - just missing some of the finer points.
try:
while ($row = mysql_fetch_array($sharelist)) {
var_dump($row);
}
...and it should be obvious what's happening.