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.
Related
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!
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'm trying to run this function but can't get it to work, what am I doing wrong (output says my fetch array arg is invalid)?
$link = mysql_connect('localhost', 'over_app', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) { echo "Your Pic ID: $row['PicID']"; }
Note The Pic table is in the Pics database and the Seen table is in the SeenPics database
mysql_query returns a boolean false is the query fails .. do this
$result = mysql_query("SELECT Pic.PicID FROM Pic LEFT JOIN Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) {
echo "Your Pic ID: $row['PicID']";
}
Don't use mysql_* See this comment
Update
Now that we have the full facts - ie your attempting a join across multiple databases ... the problem is that your query isnt complete ... try this :
SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID
At the moment I've got the following code:
$forummost = mysql_query("
SELECT door,
COUNT(door) AS doorCount
FROM forum_posts
GROUP BY door
ORDER BY COUNT(door)
DESC
");
$forummostc = mysql_num_rows($forummost);
$forummostf = mysql_fetch_assoc($forummost);
Can somebody explain me why mysql_num_rows($forummost) doesn't work?
Thanks in advance!
GROUP BY selects only the different values of that column in mysql. In your case, as pointed out, it selects as many rows as different door values are there.
mysql_num_rows counts the number of rows returned by the query. Your query is what is the count for each door so only one row for each door is returned.
try this, I tested, it works..
<?php
$link = mysql_connect('localhost','root','');
if (!$link)
{
die('Could not connect to MySQL: ' . mysql_error());
}
echo 'Connection OK'."</br>";
$forummost = mysql_query("
SELECT door,
COUNT(door) AS doorCount
FROM forum_posts
GROUP BY door
ORDER BY COUNT(door)
DESC
");
$forummostc = mysql_num_rows($forummost);
if ($forummostc == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($forummost)) {
echo $row["door"];
echo $row["doorCount"];
}
mysql_free_result($forummost);
mysql_close($link);
?>
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?