I'm fairly new to php and mysql and I'm on the home stretch of finishing my page but I've been banging my head on the keyboard all day trying to figure out how to fix this problem. I've set up a php script to run as a cron even every 24 hours. The script assigns a random number between 10 and 30 to each field in my table. That works fine and every time I load the script the values change.
The problem I'm having is when I try to use those values. The result keeps printing as the word Array instead of the number in the table. So I'll give you some snippets of the code I'm running here. This is the cron event.
?php
$con = mysql_connect("localhost","username","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$random= rand(10, 30);
mysql_query("UPDATE winners SET pool= '$random'");
mysql_close($con);
And here is the script to call up the values.
php?
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row % 2 )
{
echo "<h4>Result 1</h4>";
echo "$row";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
I've tried every possible variation I can think of to this code but all I can get is it to echo either Array or Resource #9. Any insight into this would be greatly appreciated.
You do not want to echo the content of $row, which contains all data returned for the current line you've fetched from the database when calling mysql_fetch_array().
Instead, you want to access the content of $row's pool item :
echo $row['pool'];
You should probably take a closer look at the manual page for mysql_fetch_array(), and the examples it contains.
Note that you'll probably also want to modify the condition in the following line :
if ( $row % 2 )
You probably don't want the test to be done on $row, but on an item it contains.
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
echo "$row['pool']";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
}
Hope this helps.
Related
Good day,
I'm a noob at PHP and MySQl. Looking to be pointed in the right direction.
I have a MySql table with 5 columns. Each column represents a specific set of data. All numerical.
I want to write PHP code which takes each value in a single column and puts it into an array that I can then modify.
I don't know how to get each column as a separate array. Should I get an array of all the rows and then do something extra to separate each of the 5 numbers in each row into 5 separate arrays?
**
require_once 'login.php';
echo $db;
$conn = mysqli_connect($hn,$un,$pw,$db);
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$query = "SELECT xox FROM tablex WHERE id = '1'";
$result = $conn->query($query);
if(!$result) die ("Database access failed: " . $conn->error);
$rows = mysqli_fetch_row($result);
while($rows){
echo $rows['index'];
}
echo $rows[0];
?>
$statement = **yourDatabaseConnection**->query('SELECT column1 FROM table');
$datas = $statement->fetch();
$datas will be an array of your column1 datas
Try reviewing the php documentation, here is a good place to start...php:mysql_fetch_row
Take a look at the example below, if is not what you looking for, please edit your question and provide the approach you are taking (your code) so that we have a better understanding where you are having issues.
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
Thank you all for you help! I received some phenomenal help about 17 days ago which saved my *!. I am still learning PHP and have run across a new problem. I have a MYSQL database field called "Tracking" this is a long text field in which we want to input tracking numbers (for shipping) separated by commas . We ship some times 20-50 individual boxes in the same order.
The following script outputs to an HTML table the number of boxes being shipped (EX Box 1 of 12, Box 2 of 12...etc) and then the value of the tracking number field. I need to modify it so it parses at the "comma" in the Tracking field and includes the tracking number for each box. So Box 1: Tracking Number "take first tracking number and end at comma", So Box 2: Tracking Number "take second tracking number and end at comma"....etc...does this make sense?
Please provide whatever help possible, would greatly be appreciated!
Script:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('***','***','***','***');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db116223_wmi");
$sql="SELECT * FROM tbl_purchase_order WHERE purchase_order_id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
for ($i=0; $i<$row['product_quantity']; $i++) {
$my_val=1;
$x += 1;
echo "<table>";
echo "<tr>";
echo "<td width=400><br>Box $x of ". $row['product_quantity'] ." Tracking Number:". $row['Tracking'] ."</td>";
echo "</tr>";
echo "</table>";
}
}
mysqli_close($con);
?>
JB
As jjonesdesign mentioned, you need to use explode to break the tracking number field up at each comma. explode creates an array of values, each split out of the Tracking field. Look below for the solution:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('***','***','***','***');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db116223_wmi");
$sql="SELECT * FROM tbl_purchase_order WHERE purchase_order_id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
$myTracking = explode(',',$row['Tracking']); // get an array of tracking numbers. Array element starts with 0
for ($i=0; $i<$row['product_quantity']; $i++) {
$my_val=1;
$x += 1;
echo "<table>";
echo "<tr>";
echo "<td width=400><br>Box $x of ". $row['product_quantity'] ." Tracking Number:". $myTracking[$i] ."</td>";
echo "</tr>";
echo "</table>";
}
}
mysqli_close($con);
?>
I've searched google and on here and some info has been useful but mostly not so useful and still can't manage to query my database using php.
I'm very new to coding so thought it best to find an answer based on my code rather than other peoples slightly different problems.
I have a small database, people fill in a form who wish to hire a bus. i have created a calendar and wish to print out for each day what time and for how long there is a hire, if there is one, based on the data people have entered which has been sent to my database.
The whole page shows but the calendar contains an error within its table saying:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2013-03-01' at line 1"
even if i delete the "where date=.." part of the query it continues to show this.
ive tried several different methods of writing the code out, including a loop and not including one but im not really sure what im meant to do.
EDIT: MY CODE IS NOW:
<?php
$con = mysql_connect("localhost","user","password");
$result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'");
$row = mysql_fetch_assoc($result);
$Time = $row['TIME'];
$Length = $row['LENGTH'];
echo "Time: " . $TIME . "<br/>";
echo "Length: " . $LENGTH . "<br/>";
?>
I now get the error "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Users\Laura\Documents\COMPY\server\www\calendar.php on line 123"
It shows the page, calendar and "time" and "length" but with the error and no data
EDIT
<?php
$con = mysql_connect("localhost","user","password");
mysql_select_db("busassociation", $con);
$result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'");
while ($row = mysql_fetch_assoc($result)){
$time = $row['time'];
$length = $row['length'];
echo "time: " . $time . "<br/>";
echo "length: " . $length;
}
?>
use backticks
$result = mysql_query("SELECT `time`, `length` FROM `hire` WHERE `date`= '2013-03-01' ");
EDIT.
your are fetching your query two times.
$row = mysql_fetch_row($result); <------- delete this line
while( $row = mysql_fetch_row($result) ){
EDIT.2
i think u dont reconize the diference between
mysql_fetch_assoc() and mysql_fetch_row()
if you use mysql_fetch_assoc() it will be correct in your code.
but if u use mysql_fetch_row() as u have done in your code u must echo values like that
$Time = $row['1']; // suppose that `TIME` is the second column
$Length = $row['2']; // suppose that `LENGTH` is the third column
EDIT.3
<?php
$con = mysql_connect("localhost","user","password");
mysql_select_db("your_db", $con); <---------replace by your database
$result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'");
while ($row = mysql_fetch_assoc($result)){
$Time = $row['time'];
$Length = $row['length'];
echo "Time: " . $Time . "<br/>";
echo "Length: " . $Length . "<br/>";
}
?>
I want to echo the contents of a table (a product list) yet still have control over the individual fields if possible. While loops did not work as you cannot then go to pick how each field is displayed.
What I am after is to create a product list which then has each record as a hyperlink setting a variable to the product id. Something like:
www.domain.com/catalog/product.php?productid=1
If there isnt a way to echo the fields individually within each row, how would I be able to get this outcome without adding the link into the database itself for all the products?
Its fairly easy
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Product_List");
while($row = mysql_fetch_array($result))
{
echo "<a href='www.domain.com/catalog /product.php?productid=".$row['productid']."'>".$row['productid']."</a>";
echo "<br />";
}
mysql_close($con);
?>
You'll still need an while loop. Consider this:
<?php
$getRows = mysql_query("SELECT id FROM products");
while($row = mysql_fetch_assoc($getRows)) {
echo 'Product link';
}
I've been trying for hours to figure out how to put a link into the following text output via PHP echo(). I basically want to make the title field I'm pulling from my events table (as seen in #4 in the code below) to come back into the browser as a link instead of just text...
the original code that brings back the event title:
<?php
// 3. Perform database Query to bring list of events
$result = mysql_query("SELECT * FROM events", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
// 4. Use returned Data
while ($row = mysql_fetch_array($result)) {
echo $row ["eventtitle"]."<br/>".$row["eventdesc"]."<br/>";
}
?>
How would I go about getting that $row["eventtitle"] to appear in the browser as a link? Let's say if the link was just "eventprofile.php". This is probably an easy fix, but I've been getting a million errors with trying different things with <a href>s.
<?php
$result = mysql_query("SELECT * FROM events", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo "".$row["eventtitle"]."<br/>".$row["eventdesc"]."<br/>";
}
?>
add an anchor tag for it!!
echo "" . $row['eventtitle'] . '' . "<br />" .$row["eventdesc"]."<br/>";
And thats it.