I am using following code
$con=mysql_connect('localhost','admin',"password");
$db=mysql_select_db('DbName',$con);
$query="SELECT COUNT(shuffel.clientid) as total_users FROM shuffel";
$result=mysql_query($query);
$total=mysql_fetch_array($result);
$total =$total['total_users'];
if($total>=2)
{
$result=$total/2;
$length =round($result);
if ($length > 10)
$length = 10;
for($i = 0; $i < $length; $i++)
{
$data = "SELECT * FROM shuffel ORDER BY RAND() LIMIT 2";
$shuffeluser = mysql_query($data);
int count = 0;
while($row = mysql_fetch_assoc($shuffeluser))
{
$my_array[] = $row;
count ++;
}
}
Now i want to get something like
$firstUserId = $my_array[0]->id;
However Everhthing just returns empty.
What is wrong in above code
mysql_fetch_assoc() returns array, not object so go get id you should use $my_array[0]['id'].
Also think about moving to PDO or mysqli_* because mysql_* is deprecated.
Related
i want to show Random Item with Select by WHERE code = $Code
But if he finds like 10 Items, i want to show only 1 by 1. All 10 must show in a Row, Random Order with a 5 seconds timer. The Problem is, i dont will show the same Item again and again. If the 10 Items are done, he must look again to database and start again
I already tried to do it with putting all in array but my skills are low :(
Thats my try...
$result = $db->query( 'SELECT * FROM ads WHERE zipcode = "'.$zipcode.'";');
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
$count = mysqli_num_rows($result);
for($i = 0; $i < $count; $i++) {
$random = rand(0, $count);
$count -= 1;
$result[] = $data[$random];
echo json_encode($data);
unset($data[$random]);
}
How can one get the table names from such a query using PHP and the query is run with MYSQLi
Example
SELECT table1.field1, table1.field2, table2.field3, table2.field4 FROM table1,table2
Result
array(table1,table2)
This is the code Im using but it only get the name of the first table
if (mysqli_num_rows($result) > 0) {
$numOfFields = mysqli_num_fields($result);
for ($i = 0; $i < $numOfFields; ++$i) {
$tableinfo = mysqli_fetch_field_direct($result, 1);
var_dump($tableinfo->orgtable);
}
}
Thanks in advance Im not so good at PHP
Thanks
You have to remove the hard coded number 1 with the variable $i, like this way:
Try it:
for ($i = 0; $i < $numOfFields; ++$i) {
$tableinfo = mysqli_fetch_field_direct($result, $i);
var_dump($tableinfo->orgtable);
}
I have this code:
$items_query = mysql_query('SELECT * FROM Items_Orders NATURAL JOIN Items
WHERE order_id="'.$order->order_id.'"');
if(!$items_query)
{
echo 'MySQL error: '.mysql_error();
die();
}
//add each item to the order
while($items_row = mysql_fetch_array($items_query))
{
echo "item: ";
for($i = 0; $i < count($items_row); $i++)
{
echo $items_row[$i];
}
}
When the elements of the items_row are printed, my code iterates beyond the bounds of items row. I am confused at why this is. I explicitly define i to only iterate up to the size of items_row. What's going on here?
You are counting double length, because by default, you are getting named an numbered values. Try the following
while($items_row = mysql_fetch_array($items_query, MYSQL_NUM))
{
echo "item: ";
for($i = 0; $i < count($items_row); $i++)
{
echo $items_row[$i];
}
}
See offical docs
P.S. mysql_* is deprecated, look into PDO object.
Try this:
while($row = mysql_fetch_array($query,MYSQL_NUM)){
$count = count($row);
for($i = 0;$i < $count;$i++){
echo $row[$i];
}
}
But your code is very dirty, why you do not use PDO ? Is the fast way how to make escaped queries... PDO manual
I'm querying a database for names that are numbered 1-26 alphabetically. I have the following code, but since HTML is structured tr then td, the table appears alphabetically by row as opposed to by column. How can I make it appear in order by column?
$query = mysql_query("SELECT name FROM people WHERE main=1 ORDER BY id");
$i = 0;
while($result = mysql_fetch_array($query)) {
$name = $result['name'];
if ($i % 5 == 0) echo "<tr>\n";
echo "<td width=\"150\">";
echo "".$name."<br />";
echo "</td>\n";
$i++;
if ($i % 5 == 0) echo "</tr>\n";
};
alpha beta charlie
delta echo foxtrot
vs.
alpha charlie echo
beta delta foxtrot
Also, I'm open to reworking the code if there's a more efficient way.
You could just access the output array in strides. Compute how many rows you need as the number of results divided by 5, and use the row count as the stride.
$ncols = 5;
$nrows = $nresults / $ncols + ($nresults % $ncols == 0 ? 0 : 1);
for ($i = 0; $i < $nrows; $i++)
{
// start row
for ($j = 0; $k < $ncols; $j++)
{
// print $results[$nrows * $j + $i]
}
// end row
}
You'll have to transfer your query results into an array $results first. Since you'll have to know the total number of results, this is sort of mandatory, though I'd be curious if anyone has a solution that can work while fetching the results.
Update: See Justin's answer for a cool solution that grows the output while fetching the query results line by line. Since it's currently being worked on, here's a summary (credits to Justin):
$nresults = mysql_num_rows($query);
$ncols = 5;
$nrows = (int) ceil($nresults / $ncols);
$i = 0; $cols = array_fill(0, $nrows, "");
while ($result = mysql_fetch_array($query))
$cols[$i++ % $nrows] .= "<td>$result['name']</td>";
echo "<tr>" . implode("</tr><tr>", $cols) . "</tr>";
Edit:
After the discussion in the comments between myself, Kerrek SB and the OP bswinnerton, the following code seems to be the most effective:
$columns = 3;
$rowcount = mysql_num_rows($query);
$rows = ceil($rowcount / $columns);
$rowdata = array_fill(0, $rows, "");
$ctr = 0;
while ($result = mysql_fetch_array($query))
$rowdata[$ctr++ % $rows] .= '<td>'.$result['name'].'</td>';
echo '<tr>'.implode('</tr><tr>',$rowdata).'</tr>';
This will create three columns, filled vertically (my original answer would create three rows). It also properly initializes the array (preventing PHP warnings), yields a correct row count for result counts that aren't divisible by the column count, and incorporates Kerrek's clever "calc-row-in-the-subscript" trick.
Original Post:
You could use arrays and implode() This way, you only have to make one pass through your results:
$row = 0;
$rows = 3;
$rowdata = array();
while($result = mysql_fetch_array($query))
{
if ($row >= $rows) $row = 0;
$rowdata[$row++] .= '<td>'.$result['name'].'</td>';
}
echo '<tr>'.implode('</tr><tr>',$rowdata).'</tr>';
I'm trying to output something special with the first row but it seems to output on every row, here is the code im using:
$query = mysql_query("SELECT * FROM chat WHERE id > '".$_GET["latest"]."' ORDER BY id DESC LIMIT 0, 20");
$number = mysql_num_rows($query);
$i = 1;
while ($row = mysql_fetch_assoc($query)) {
echo "<div class='babble' style='width:130px;overflow:hidden;margin:auto;'><font color=#ff4355><b>".$row['author']."</b></font><font color=#ff4355>:</font></b> ".$row['babble']."</div>";
if($i = 1)
{
echo "<script type='text/javascript'>newestid=".$row['id']."</script>";
$i = 2;
}
}
You need to use the comparison operator ==, not the assignment operator = in your if statement.
if ($i == 1)