Im trying to set a php variable to a field and row of a database. I use:
$moped_select = "SELECT * FROM moped_details WHERE moped_ID='$_SESSION[moped_number]'";
$selected_moped = $db->query($moped_select);
if (($selected_moped->rowCount()) >= 1) {
foreach($selected_moped as $row){
$moped_rate = $row['rate'];
}
This doesn't set $moped_rate as the field and row, I have seen other people use this line before and it works. Whats even weirder is that I use a similar line to this to place data in a form. Which looked like this (keep in mind it is inside a foreach(x as $row) loop:
echo "<td>".$row['model']."</td>";
This echos out correctly so im kinda confused why Any suggestions would be appreciated <3.
Your code should as below
$db=new PDO('mysql:host=localhost','username','password');
$moped_select = "SELECT * FROM moped_details WHERE moped_ID=?";
$selected_moped = $db->prepare($moped_select);
$db->execute([$_SESSION["moped_numberf"]]);
if(($db>rowCount()) >= 1) {
while($row=$selectedmoped->fetch(PDO::FETCH_ASSOC){
$moped_rate = $row['rate'];
}
Related
I am trying to solve a mysteries code that writing a number in a choice I will get all opposite of that number
I did almost done with that the only problem with it I might have the opposite recalled as he does have another recorded in another course
In more word my PHP about courses when I do ask MY PHP course it well give me all people how did not take course 1 but some people were recalled as they have other courses
The Problem when I choose For Example Course X I do get All Who Did not Take Course X But Some where Called From The same Table cause They Do Have Taken Other Courses In the same table
I did resolve The first Problem Using the following code
if(isset($_GET['course_guid']) && $_GET['course_guid']!="" )
$f.=" AND s.course_guid != '".$_GET['course_guid']."'";
And I Did Create Another page Select.php To Omit All Identified People from Orig.php using The Following Code
<?php
if(isset($_GET['id']))
foreach($_GET as $key => $post)
$_GET[$key]=m($post);
$ff="";
if(isset($_GET['id']) && $_GET['id']!="")
$ff.=" AND cs.id = '".$_GET['id']."' ";
$prns =(isset($_GET['prn'])? $_GET['prn']:'');
if(isset($prns) && $prns!="")
$ff.=" AND cs.prn = '".$prns."' ";
$course_guids =(isset($_GET['course_guid'])? $_GET['course_guid']:'');
if(isset($course_guids) && $course_guids!="")
$ff.=" AND cs.course_guid = '".$course_guids."' ";
$from_time=system_time();
$date_from= date("d",$from_time)."-".date("m",$from_time)."-".date("Y",$from_time);
$date_to=date("d",system_time())."-".date("m",system_time())."-".date("Y",system_time());
if(isset($_GET['alldates']) && $_GET['alldates']=="on") {
$t_f=mktime(0,0,0,6,1,2011);
$t_t=system_time();
} else {
$t_f=mktime(0,0,0,substr($date_from,3,2),substr($date_from,0,2),substr($date_from,6,4));
$t_t=mktime(23,59,59,substr($date_to,3,2),substr($date_to,0,2),substr($date_to,6,4));
}
$query="SELECT cs.id,cs.prn,cs.course_guid FROM courses_attend cs
WHERE cs.time BETWEEN $t_f AND $t_t
$ff
";
//echo "$prns";
$rs22 = mysql_query($query) or die(mysql_error());
// $total22 = mysql_num_rows($rs22);
/*
while($row22=mysql_fetch_assoc($rs22))
{
$prnss =(isset($row22['prn'])? $row22['prn']:'');
print($prnss);
}
*/
?>
print($prnss); // this should be the code needed to be recalled In Orig.php
and It should Be Called Inside a loop mysql_fetch_array which is cannot be Used In orig.php Cause I do already Have a structure and Another Loop In that page Cause real Output Interface is there
as far as I do know I do think function callback() would be a solve on the other hand I do not know how to create one
// Answer
for ($i= 0; $i < mysql_num_rows($rs22);$i++) {
$row22 = mysql_fetch_assoc($rs22);
$prnss = $row22['prn'];
// echo "$prnss"; // return print all Omitted data
// echo "<br />\n";
$f.=" AND s.prn != '".$prnss."' ";
}
Solved By Adding This In Orig.php
On the other hand and after testing Select.php DATA can be added TO the Orig.php
Best regards
I am creating a baseball website and I am learning php as I go. I have hit a snag with something I am trying to do; here is what my current code shows:
here is my code:
When I load my files that update the scores, if the game hasn't started the scores are NULL. Is there a code to change it to 0 without having to change it in my database. Any info will be helpful
In your select statement you can use the IFNULL(expr1, 0) function, but you will need to list your columns explicitly instead of using select *. So it would be roughly:
select
game_date,
game_time,
...,
IFNULL(AWAY_SCORE, 0),
IFNULL(HOME_SCORE, 0)
FROM scoreboard;
Yup, by way of PHP. It could also be done in the database query
while($row = $result->fetch_assoc(){
$ascore = $row['score'];
$hscore= $row['score'];
if($ascore == ""){
$ascore = 0;
if($hscore== ""){
$hscore = 0;
}
REST OF YOUR CODE HERE
}
}
So my brain is having trouble functionning this morning and I can't figure out how to create a Top list.
I have my query like so
$moyennepisode = $conn->prepare('SELECT * FROM show_moyenne, shows WHERE shows.id = show_moyenne.show_id ORDER BY moyenne DESC LIMIT 100');
$moyennepisode->execute();
while ($show = $moyennepisode->fetch()) {
somecode }
Which is all fine but I simply want to output the position so I tried a for loop but I can't seem to figure out where to place it. I tried an associate array but I failed at that, too.
Can anyone recommend how to do that ? And if there might be a way to do have the position from mmysql instead of php ?
Any help appreciated. Thanks!
$position = 1;
while ($show = $moyennepisode->fetch()) {
Output $position;
$position++;
somecode;
}
OK i have a php link which is made up of several variables
<a href=\year.php? manufacturer='.$manufacturer.'&fuel_type='.$fuel_type.'&model_type='.$model_type.'>'.$model_type.'</a>
The whole code is really long as it has a lot of pagination, so i will just include the basic query and the loop part.
$query1 = "SELECT Distinct model_type from $tableName where manufacturer='$manufacturer' AND fuel='$fuel_type' LIMIT $start, $limit";
$result = mysql_query($query1);
And then the bottom part where i get and show the results.
$count = 0;
$max = 2;
while($row = mysql_fetch_array($result))
{
$model_type = $row['model_type'];
$count++;
echo '<td class="manu"><div align="center">'.'<a href=\year.php? manufacturer='.$manufacturer.'&fuel_type='.$fuel_type.'&model_type='.$model_type.'>'.$model_type.'</a>'.'</div></td>';
if($count >= $max){
//reset counter
$count = 0;
//end and restart
echo '</tr><tr>';
}
}
now this works fine except when i take the mode type variable from the database it shows as 1 series, however when it is passed in this link it only gets the 1 and doesn't pick up the series.
Thanks
try to pass it like:
'.urlencode($model_type).'
Try To access it on next page with urldecode($_REQUEST['$model_type'])
I am not shure but you probably have a missing encoding Problem.
try this:
'.$model_type.'
Its url_encoding the values so your url doesnt get broken by ',",spaces and so on.
additional Hint:
Enclose your URL with " or ' so you do not get problems at the end.
I code a weekly trivia program for one of my clients through facebook.
I have a bit of code commented out where we display the winner when we need to. Currently I just remove the comment brackets and update when it's time to display. I'm trying to make this so someone non-savvy can handle updates so I've moved my code into an include:
winner-display.php
I am trying to write a function so that if the winner is set in MySQL, it includes the file in-line, and if the winner field is empty in the database, it does not.
Here is what I have so far, any ideas?
<?php
$target="3";
$myDataID = mysql_query("SELECT topic_desc from ref_links WHERE ref_categories_id = '$target' AND topic_name = '$property'", $connectID);
while ($row = mysql_fetch_row($myDataID)) {
$displayvalue = $row ['topic_desc'];
}
if ( $displayvalue != 'null') {
include('../includes/winner-display.php');
} else {
}
?>
Ok, thanks for helping guys, got it to work as:
<?php
$target="3";
$myDataID = mysql_query("SELECT topic_desc from ref_links WHERE ref_categories_id = '$target' AND topic_name = '$property'", $connectID);
while ($row = mysql_fetch_row($myDataID)) {
foreach ($row as $field) {
if ($field != null) {
include('../includes/winner-display.php');
}
}
}
?>
You can definitely put an include within an if. That solution that you posted should work as you would like it to, although I personally would have used a function instead of a completely separate file to include (although that is personal preference).
All you have to do to make it work is remove the quotes around 'null'.
<?php
$target="3";
$myDataID = mysql_query("SELECT topic_desc from ref_links WHERE ref_categories_id = $target' AND topic_name = '$property'", $connectID);
while ($row = mysql_fetch_row($myDataID)) {
$displayvalue = $row ['topic_desc'];
}
if ( $displayvalue != null) {
include('../includes/winner-display.php');
}
?>
Keep in mind that if your query returns more than one row, only the last row will be retained. I don't know if that is the functionality you want (in which case, there are some changes you could make, just ask me to edit my answer), but I didn't change that.