Getting multiple table names from a single MYSQLi query - php

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);
}

Related

looping multidimentional array on pivot table laravel

Excuse me, I want to ask about the iteration step.
i have productdata like this
and criteria data
and alternative data as a liaison / place for the main data from the two tables above
i want to make iteration or loop $nilaiprefensi like below
with calculations like this
$nilaiprefensi[0]= [["A1,A2"],[2-4],[5-9],[10-9],[3-9],[6-8],[4-8],[120-120]]
$nilaiprefensi[1]= [["A1,A3"],[2-3],[5-8],[10-6],[3-8],[6-16],[4-9],[120-155]]
..
$nilaiprefensi[5]= [["A2,A1"],[4-2],[9-5],[9-10],[9-3],[8-6],[8-4],[120,120]]
I've tried several times to make this loop function, but it doesn't work, here's my latest code
for ($i = 0; $i <$loop; $i++) { //30
for ($j = 0; $j < count($products); $j++) {
if ($i !== $j) {
$p[$i][$j][0] = ('A' . $i + 1) . ' , A' . $j + 1;
for ($k = 1; $k < count($kriteria); $k++) {
$p[$i][$j][$k]=$alternatif[$k-1]->kriteria_data."-".$alternatif[$i]->kriteria_data;
}
}
}
}
dd($p);
What is the correct looping function to get the expected result, please help, thanks 🙏🙏

How to reduce search result when you have more than one word?

i need to write a search engine. And i had occurred some prbolems with it.
so for now my query looks like this.
SELECT * FROM umowy WHERE keywords LIKE ('%propanek%' OR '%seba%')
And i making this, with this example of code. (two words for now)
$szukaj = $_POST['szukaj'];
$szukaj_trim = trim($szukaj);
$szukaj_array = explode(" ",$szukaj_trim);
$szukaj_length = count($szukaj_array);
if ($szukaj_length === 1 ) {
$constr = "('%${szukaj_array[0]}%')";
}
else{
for($i = 0; $i <= $szukaj_length; $i++){
if($i == 0){
$constr = $constr."('%${szukaj_array[${i}]}%'";
}
if($i > 0 && $i < ($szukaj_length-1)){
$constr = $constr." OR '%${szukaj_array[${i}]}%'";
}
if ($i == ($szukaj_length-1)) {
$constr = $constr." OR '%${szukaj_array[${i}]}%')";
}
}
}
$query="SELECT * FROM umowy WHERE keywords LIKE $constr";
Is there way to reduce a result output by adding more words in search input?
Now it's working like "more words, more content to show". But i need something opposite, more words, the more accurate result is.
(sorry for my not perfect english)

Undefined offset when printing elements of an array

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

Getting variables after Randomly selecting two rows in PHP

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.

PHP variable additions

Hey does anyone know a reason why this is not working? its not calculating any of the additions and just entering 0 into the database. Any help would be great, thank you!.
$member_id = //users member id in database//
$track = //the track results being updated//
$engine = //the engine id from the members table in database//
$engine_points_system = array();
$engine_points_system["qualpos1"] = 30;
$engine_points_system["qualpos2"] = 20;
$engine_points_system["qualpos3"] = 18;
$engine_points_system["qualpos4"] = 17;
$engine_points_system["qualpos5"] = 16;
$enginepoints = 0;
$qualifyingpoints = 0;
$results_query = mysql_query("SELECT pos_1, pos_2, pos_3, pos_4, pos_5
from engine_qualifying_results WHERE track_id = '$track'")
or die ("Failed to update" . mysql_error());
$row = mysql_fetch_array($results_query);
$enginequalifying = array();
for ($i = 1; $i <= 5; $i++) {
$enginequalifying["pos$i"] = $row['pos_$i'];
}
for($i = 1; $i <=5; $i++) {
if($engine == $enginequalifying["pos$i"]){
$enginepoints += $engine_points_system["qualpos$i"];
$qualifyingpoints += $engine_points_system["qualpos$i"];
}
}
$results_query = mysql_query("INSERT INTO member_results (member_id, engine_points)
VALUES ('$member_id', $enginepoints')")
or die ("Failed to update" . mysql_error());
$enginequalifying["pos$i"] = $row['pos_$i'];
In this line you have 'pos_$i'. This is the literal string 'pos_$i'. You should use "pos_$i" instead.
$enginequalifying["pos$i"] = $row["pos_$i"];
UPDATE:
In your code $enginequalifying is redundant, and not needed. You can just use $row in its place.
for($i = 1; $i <=5; $i++){
if($engine == $row["pos_$i"]){
$enginepoints += $engine_points_system["qualpos$i"];
$qualifyingpoints += $engine_points_system["qualpos$i"];
}
}
Also, as #ax. points out, you have an extra ' (or a missing ') in your INSERT.
$results_query = mysql_query("INSERT INTO member_results (member_id, engine_points)
VALUES ('$member_id', '$enginepoints')")
or die ("Failed to update" . mysql_error());
Look at this code:
<?php
$i = 5;
print "i is $i";
print "\n";
print 'i is $i';
?>
You'd expect it to print:
i is 5
i is 5
But instead, it will print:
i is 5
i is $i
This happens because when the string is wrapped in single quotes, $i is not evaluated. It is just the string $i.
To fix the code, try replacing this line:
$enginequalifying["pos$i"] = $row['pos_$i'];
With this line:
$enginequalifying["pos$i"] = $row["pos_$i"];
Quotes make a difference.
And by the way, ESCAPE YOUR SQL!!!. Please?
Not an answer, but too ugly to put into a comment: You could bypass the entire loop to build the enginequalifying array by simply doing:
SELECT pos_1 AS pos1, pos_2 AS pos2, etc...
for your query, then simply having:
$enginequalifying = mysql_fetch_assoc($result);
It's a waste of CPU cycles to have PHP fetch/rename database fields for you when a simple as alias in the original query string can accomplish the exact same thing.
And incidentally, this will also remove the string-quoting error you've got that Rocket pointed out in his answer.
I dont think it is possible to say without knowing what you have in your database.
But I can tell you that you have a syntax error in the last SQL query ($enginepoints ends with a quote).

Categories