Why the first 2 querys don't return anything? - mysql

For some reason the first two query's don't show anything. I made it so, that you press a submit button to post your input, and then have it controlled and added to the database. But it doesn't work.
$sql = "SELECT * FROM rubrieklink WHERE rekeningnummer = '".$_POST["rekeningnummer"]."'";
$resultrubrieken = $mysqli->query($sql);
$sql="INSERT INTO betalingen(
bedrijf
,rekeningnummer
,prijs
,rubriek)
VALUES('".$_POST["name"]."'
,'".$_POST["rekeningnummer"]."'
,".$_POST["prijs"]."
,'".$resultrubrieken["rubriek"]."')";
$result = $mysqli->query($sql);
$resultbetalingen = $mysqli->query("SELECT * FROM betalingen");
if ($resultbetalingen->num_rows > 0) {
echo "<center><table><tr><td>bedrijf</td><td>rekeningnummer</td><td>prijs</td><td>rubriek</td></tr>";
while($betalingen = $resultbetalingen->fetch_assoc()){
echo"<tr><td>".$betalingen['bedrijf']."</td><td>".$betalingen['rekeningnummer']."</td><td>".$betalingen['prijs']."</td><td>".$betalingen['rubriek']."</td></tr>";
}
echo"</table>";
}

is rekeningnummer an INTEGER or a string if so you must quote your strings same goes for the insert for example $_POST["name"] is not quoted
PS also why is the INSERT INTO doubled in your query?

Related

Set local variable for current row in SELECT

I'm trying to use a variable from a row that is being selected from one table in a SELECT function targetted towards a second table. I tried doing this:
while ($row = mysqli_fetch_assoc($result)) {
$sender = $row['sender_id'];
$sql = "SELECT * FROM users WHERE user_id='$sender'";
$result = mysqli_query($conn, $sql);
while ($row2 = mysqli_fetch_assoc($result)) {
echo $row2['user_uid'];
}
}
I understand that this is not how you set a variable for each row, but I have no clue how it should be done otherwise. At the moment it only displays the username of the sender of the first notification in my inbox database, not the rest.
If anyone knows how to set a variable per row, please let me know. Thank you in advance.

MySQL / PHP: cannot perform two separate queries on same database?

I have one database with two tables: "music" and "agenda".
But for some reason once I have queried one table, I cannot perform a similar query on the other table. Or in any case, its variables are empty.
I'd think I could just keep the connection open and perform a second query after the first "while". Like so:
<?php
mysql_connect('localhost', 'root', 'root');
mysql_select_db('erikverwey');
$result = mysql_query("SELECT * FROM agenda ORDER BY date DESC LIMIT 0, 2") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$count++;
$date[$count] = $row['date'];
$time[$count] = $row['time'];
$place[$count] = $row['place'];
$venue[$count] = $row['venue'];
$who[$count] = $row['who'];
$concert[$count] = $row['concert'];
$urlvenue[$count] = $row['urlvenue'];
}
$result2 = mysql_query("SELECT * FROM music ORDER BY id LIMIT 0, 5") or die(mysql_error());
while($row = mysql_fetch_array($result2)) {
$count++;
$song[$count] = $row['song'];
$artist[$count] = $row['artist'];
$duration[$count] = $row['duration'];
$url[$count] = $row['url'];
}
mysql_close();
?>
But no. In this case, all the variables from the table "music" remain empty.
I've been looking for an answer, but no luck. I'm still new to MySQL, though, so apologies beforehand if this is standard stuff. Thanks!
I found the glitch. Because the counter "$count" was used a second time, it started where it left off and couldn't find any data.
Use a different counter, also in the variables (!), and all is good.

how to use getNumRows() in Joomla after a second query

I am developing a php script within the Joomla environment which queries the same table / database twice. Each time I need to know whether any matches are found.
It seemed that the best way would be to use the getNumRows(). The Joomla documentation is very specific on its use:
Miscellaneous Result Set Methods getNumRows()
getNumRows() will return the number of result rows found by the last
query and waiting to be read. To get a result from getNumRows() you
have to run it after the query and before you have retrieved any
results.
I follow this in my script. At the first query there is no problem, but the second query always throws up a warning - most likely because the getNumRows() call for the second time is after the retrieving results from the first query - which does not comply with the Joomla requirement.
Any ideas how to solve? Many thanks!
The part of my script in question reads:
$db = JFactory::getDBO();
$query = "SELECT * FROM #__art_mobiles WHERE user_agent_header='$ua'";
$db->setQuery($query);
$rowsAG = $db->getNumRows();
$replyAG = $db->loadRow();
if ($rowsAG == 0) {
//if no match check www.handsetdetection.com
//see https://www.handsetdetection.com/properties/vendormodel for current list of models in database together with headers
echo "not in local database - try external<br/>";
$prod = '';
if ($hd3->siteDetect()) {
$replyHD = $hd3->getReply();
$man = $replyHD['hd_specs']['general_vendor'];
$dev = $replyHD['hd_specs']['general_model'];
$os = $replyHD['hd_specs']['general_platform'];
echo "found in handsetdetection.com database<br/>";
//check for provisional match in local database
$query = "SELECT * FROM #__art_mobiles WHERE manufacturer='$man' AND device='$dev'";
$db->setQuery($query);
$rowsAGprov = $db->getNumRows();
$replyAGprov = $db->loadRow();
if ($rowsAGprov == 0) { **[ETC]**
I think this could be an issue with using $db->loadRow(); as getNumRows relies on an executed query.
For example you could try:
$db = JFactory::getDBO();
$query = "SELECT * FROM #__art_mobiles WHERE user_agent_header='$ua'";
$db->setQuery($query);
$replyAG = $db->query();
$rowsAG = $db->getNumRows();
And:
$query = "SELECT * FROM #__art_mobiles WHERE manufacturer='$man' AND device='$dev'";
$db->setQuery($query);
$replyAGprov = $db->query();
$rowsAGprov = $db->getNumRows();
Though I am not sure what/if the difference will be between the results returned from query and loadRow. It would be worth experimenting and seeing if this works.
Alternately, if you are only using getNumRows to see if a record exists, you could do some kind of check on your $replyAG variable instead. It again might be worth experimenting to see what loadRow returns if there are no results.
You need to add the following code before you write the the query:
$query = $db->getQuery(true);
You need to use
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->qn(array('id')))
->from($db->qn('#__social_notifications'))
->where($db->qn('status') . ' = ' . $db->q(0));
$db->setQuery($query);
$db->execute();
$resultData = $db->getNumRows();

Drupal 6 : Not able to fetch group_id(gid) from OG table

I am creating nodes pro-grammatically by fetching emails. Where I am splitting the subject of the mail for creating it for specific group & the title of the node.
Now I want to fetch the group_id by the description of the group and wrote query for it, but it's not working. Let me paste the code here..
list($group_name, $title_text) = explode(', ', $title);
$query = "SELECT * FROM {og} WHERE og_description = ' ".$group_name." ' ";
$group_details = db_query($query);
while ($group = db_fetch_object($group_details)) {
$gid = $group->nid;
}
echo $gid;
echo $gid is giving nothing. Though $group_name = 'Logo design' & gid = 1442 for it in table.
Is there anything I am missing here ?
Check out the following two pages , the examples give here does not use the single quotes around the placeholder in the query ($group_name - in your example) .
http://drupal.org/node/310072
One of the lines says "Note that placeholders should not be escaped or quoted regardless of their type" .
http://drupal.org/node/1407528
I have solved it. Here is the answer:-
$title = "ED's presentation, This content is for ed's presenation"; //This is the subject of the mail, which I am fetching.
list($group_name, $title_text) = explode(', ', $title);
$query = "SELECT nid FROM {og} WHERE og_description = '".$group_name."'";
$group_details = db_query($query);
while ($group = db_fetch_object($group_details)) {
{
$gid = $group->nid;
}
Thanks :)

Help with PHPExcel Library and mySQL data from a table

I have this script
$query = "SELECT id,last_name,first_name FROM users WHERE tmima_id='6'";
$result = #mysql_query($query);
while($row = mysql_fetch_array($result))
{
$i = 3;
$emp_id = $row['id'];
$cell = 'A'.$i;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cell, $row['last_name']. $row['first_name']);
$i++;
}
But in the .xls file it prints only one user. Why id doesnt print all of the users ? W
Thanks in advance.
I make the change you said with $sheet
$query = "SELECT id,last_name,first_name FROM users WHERE tmima_id='6'";
$result = #mysql_query($query);
while($row = mysql_fetch_array($result))
{
$i = 3;
$emp_id = $row['id'];
$cell = 'A'.$i;
$sheet->setCellValue($cell, $row['last_name']. $row['first_name']);
$i++;
}
But it still prints out only one record. And yes when i run the query in phpmyadmin it returns more than one record.
How can i print out data from mySql table.. What is going wrong ?
I am pretty sure it is because you are using a unique identifier (WHERE tmima_id='6'). It is only finding the results for that one unique identifier and displaying that. Hope this helps.
$i is being reset to row 3 every loop. Set $i=3; before the while loop, not inside it.