PHP - Moved: Mysql If Statement
This topic has been moved to MySQL Help.
http://www.phpfreaks.com/forums/index.php?topic=312323.0 Similar TutorialsThis topic has been moved to Editor Help (Dreamweaver, Zend, etc). http://www.phpfreaks.com/forums/index.php?topic=308819.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=358684.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=306275.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=322930.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=318784.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=350701.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=327011.0 I have a table with 5 records with the following "id_number" for each record in ASC order: 2, 6, 74, 86,87 There is one other field called "tag_number" and for each record in ASC order here is the data: 50670, 50077, 1234, 1235, 1236 I have a text field and if I search for a spastic "tag_number" I want to return the record but count and display what record like this: Record 3 of 5 The above 3 of 5 means I searched for "1234". I'm not sure what loop I need and how to count the records, any help would be great! Here is what I'm thinking: $id_number = 74; $result = db_query("select * from table order by id_number ASC"); $num_rows = mysql_num_rows($result); //this will provide how many records are in the table $result = db_query("select * from table where id_number = '$id_number' order by id_number ASC"); for ($i = 1; $i == $num_rows; $i = $i + 1) { if ($result['id_number'] == $id_number) { This topic has been moved to Other Programming Languages. http://www.phpfreaks.com/forums/index.php?topic=354391.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=312377.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=342336.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=315503.0 Hi, I'm having a slight issue with some coding (see below). It worked a few minutes ago before I add "Packages" & "Safety & Technology" titles to the MySQL database. Hopefully the attached image has worked, but if it hasn't go to www.bikescarsandvans.co.uk/test.php and select the first Audi A3 additional extras drop down menu and you'll see whats going wrong. Code: [Select] $query_title = "SELECT * FROM extras JOIN car_to_extra ON (car_to_extra.extras_id = extras.id) WHERE car_to_extra.car_id = '{$car_row['id']}' ORDER BY extras.price ASC"; $title_results = mysql_query($query_title) or die ("Error in query: $query_title. ".mysql_error()); $current_heading = ''; print "<div class='addtional_extras'>";// ADDED TO TRY TO SORT OUT POSITIONING ISSUE while ($title_row = @ mysql_fetch_array($title_results )) { if ($current_heading != $title_row["title"]) { // The heading has changed from before, so print the new heading here. $current_heading = $title_row["title"]; print " <div class='title_tab2'>" . $title_row["title"] . "</div> "; } ?> <a class='data' href='#' onmouseout='hideTooltip()' onmouseover='showTooltip(event,"<?php print "" . $title_row["info"] . "<br/>(£" . $title_row["price"] . ")"; ?>");return false'> <?php print " <img class='extra_img' src=\"". $title_row["img"] ."\" alt='" . $title_row["img_alt"] . "' /></a> "; }// CLOSES WHILE LOOP ($title_row = @ mysql_fetch_array($title_results )) print "</div>";// CLOSES DIV IMAGE55 ive tried to write the $sql in so many ways and this looks the best and its still not working and ive checked the correct syntax but still. this is how i wrote: $sql2 = "UPDATE `tvchaty`.`episodes` SET `showid` = ".($showid).", `epname` = ".($epname).", `season` = ".($season).", `episode` = ".($episode).", `info` = ".($info).", `airdate` = ".($airdate).", `directwatch` = ".($directwatch)." WHERE `episodes`.`id` = ".($id)." LIMIT 1;"; this is the error: 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 'Prideses, `season` = 6, `episode` = 11, `info` = , `airdate` = 2010-11-01, `dire' at line 1 what could be the problem? tnx.... Hi Guys I don't know if this is possible but can someone point me in the right direction. I have a php function which takes two inputs and returns an output. for simplicity's sake let's say it's an addition function. What I want to do is use a mysql select statement to show all the rows from a database where field1 and field2 equal '3'. Here's the sort of thing I mean. function addNumbers($one,$two) { return $one + $two; } mysql_query("SELECT * FROM table WHERE 'addNumbers(field1,field2)' = '3'"); What I actually want to do is a lot more complex than this but I am trying to understand how to make the syntax work in simple terms first. Can anybody help? Many Thanks Dan I've decided to move over to using Prepared statements for security purposes, however I'm having problems with the following code. Any help or suggestions would be appreciated Output: Code: [Select] You are Logged In Fatal error: Call to a member function bindParam() on a non-object in [b]xxxxxxx[/b]/login.php on line 34 Code: Code: [Select] <?php include "functions.php"; $db_connection = db_connect(); $db_connection2 = db_connect(); $login_statement = $db_connection->prepare("SELECT COUNT(*) AS accounts FROM `accounts` WHERE `email` = ? AND `password` = ?"); $test_stmt = $db_connection2->prepare("INSERT INTO `test` (`test`) VALUES (:tst)"); login($_POST[email],$_POST[password],$login_statement); log_login($test_stmt); function login($email,$password,$login_statement){ $login_statement->bind_param("ss", $email, $password); $login_statement->bind_result($accounts); $login_statement->execute() or die ("Could not execute statement"); while ($login_statement->fetch()) { if ($accounts==1){ echo "<br/> You are Logged In <br/>"; } else{ echo "<br/>Credentials Invalid<br/>"; } } } function log_login($test_stmt){ $test_stmt->bindParam(':tst', $tst); //< ********LINE 34******* $tst="blah"; $test_stmt->execute() or die ("Could not execute statement"); } ?> Hello everyone! I'm new here on phpfreaks - Here's my problem. I get "1 <br/> 2" echoed, but not the query results. the connect() function connects and selects a table in a mysql database. Here's the code: Code: [Select] <?php connect(); $query = "SELECT `title`, `body`, `date` FROM `tutorials` ORDER BY `date` DESC" or die ("Query Error"); $counter = 0; if ($query_run = mysql_query($query)) { while ($query_row = mysql_fetch_assoc($query_run)) && ($counter <= 2) { $title = $query_row['title']; $body = $query_row['body']; $date = $query_row['date']; $counter ++; echo $counter; echo "<br/>"; echo $title; echo $body; echo $date; } } ?> If anyone has any idea, please help! - I'm been battling this for a few hours now This isn't the entire code just enough to see what I'm trying to do. Everything was working until I added the mysql update query in the if statement. Is this possible or am I doing something wrong? When I run the script it just echos "No results found" twice as $num_results = 2. Code: [Select] <?php include("../includes/connect.php"); $query = "SELECT ........ "; $result = $db->query($query); $num_results = $result->num_rows; if ($num_results == 0) { exit; } else { $i=0; while ($i < $num_results) { $row = $result->fetch_assoc(); $id = $row['id']; if ($expiration_date > $today) { ### EMAIL CODE HERE ### $update = "UPDATE model SET reminder_sent = '1' WHERE id = '$id' "; $result_2 = $db->query($update); $i++; } else { echo "No results found."; $i++; } } } ?> Hi everyone, I am currently making a page for a friend to upload a bunch of photos at a time. I was quite pleased that after a bit of googling and trial and error, I figured out how to do this so that multiple records could be added to my table with one submit button. However, my form has 10 browse iconcs. A few tests have revealed that my problem is that if only one picture is uploaded, I still get 9 entries in my database, which I don't want. My question is how can I alter the code so that a row is only populated in the database if an image is uploaded. I guess something that sort of says : if($imgx!="") { populate that row in the table } else { don't } ...and the same for $imgx002 through to $imgx010 The current query is below. Any pointers are much appreciated. Code: [Select] $query = "INSERT INTO photo_uploads (date, photo_name)" . "VALUES (NOW(), '$imgx'), (NOW(), '$imgx002'), (NOW(), '$imgx003'), (NOW(), '$imgx004'), (NOW(), '$imgx005'), (NOW(), '$imgx006'), (NOW(), '$imgx007'), (NOW(), '$imgx008'), (NOW(), '$imgx009'), (NOW(), '$imgx010')"; I'm having two issues:
(1) The correct mysql query for multiple rows
(2) The if/elseif/else to pull data from these rows, and process it based on the row data
I'm only pulling data where A=1 and B=1,2 -- so two possible entries there (call them B1 and B2). I need a php if statement to choose whether the output of B is one of two urls (b1=google,b2=bing). The actual script is far more complex, with more than just 1,2 from B. I've stripped all the excesses down to this one if/else issue and the db query. The output php doesn't matter here. And I can add more elseif once this problem is solved. <?php mysql_connect(localhost, $db_username, $db_password); @mysql_select_db($db_database) or die("No connection"); $query = "Select * FROM table WHERE column='stuff' AND parent='1,2' ORDER BY id DESC LIMIT 10"; $query_result = mysql_query($query); $num_rows = mysql_num_rows($query_result); mysql_close(); ?> <?php for($i=0; $i< $num_rows; $i++){ //start a loop $stuff = mysql_result($query_result, $i, "column"); $row = mysql_fetch_assoc($query_result, $i); if($row['parent'] == 1) { $url = 'http://google.com'; } else { $url = 'http://www.bing.com'; } ?>My own first attempt at a if/then was 500. I got help on another site to redo it (new code shown here), but the new if/else always show the else (Bing). It was also at this time that I learned that "1,2" only showed 1. Hoping that this site is far more friendly than StackOverflow. . Edited by kpmedia, 17 December 2014 - 07:22 PM. |