I have an odd problem. Basicly my page works fine however after a small bit of php, everything after those lines dont load too the page.
<?php
//GET SCHOOLS
$sql = "SELECT `id` FROM `school`";
$query = mysql_query($sql) or die(mysql_error());
$numschools = mysql_num_rows($query);
echo "<select id=\"schoolselect\" class=\"schoolselect\" value=\"Select School\">
<option id='selectschool' value = \"select\" name=\"select\">Select A School</option>
";
while($result = mysql_fetch_array($query) or die(mysql_error()))
{
$school = $result['id'];
echo "<option value = \"$school\" name=\"$school\">".$school ."</option>";
}
echo "</select>";
?>
everything before that works, the php part works but anything after that echo "select" doesnt
any help would be amazing
Never call die(mysql_error()) inside a fetch loop. mysql_fetch_array() returns FALSE when no more rows are available, so when the last row is reached, die() is called and your script will terminate leaving the </select> unclosed. You won't get an error message, but you'll be left with incomplete HTML that won't render properly in the browser.
// Don't call die(mysql_error()) in a fetch loop!
while($result = mysql_fetch_array($query))
{
$school = $result['id'];
echo "<option value = \"$school\" name=\"$school\">".$school ."</option>";
}
Chances are your script is or die()ing inside the <select> tag, which is invisible in the browser.
View the page's source, and you'll most likely see a PHP fatal error or a die message at the end.
EDIT: Or read Michael's answer, cuz I forgot how things worked. Duh :p At least this answer will help you find related problems in the future.
Related
I'm getting a weird result when i try to implement the PHP inside a HTML
The config is literally my DB connection, other scripts work well but only for this matter i couldn't figure out.
Maybe I missed out some elements.
<select name="country">
<option value="" disabled selected style="display: none;">All Japan Cities</option>
<?php
include 'scripts/config.php';
$query = "SELECT state FROM product";
$result = mysql_query($query);
$count = count($result);
if (!empty($count)) {
while($row = mysql_fetch_array($result))
{
$state = $row['state'];
echo "<option value='$state'> $state </option>";
}
} else {
echo '<option>No data</option>';
}
?>
</select>
I keep on getting no data for my select statement where I have 3 results in my db.
I don't think you can do a count() on a mysql result set like that.
Try using mysql_num_rows instead, like this:
....
$count = mysql_num_rows($result);
if (!empty($count)) {
....
Also, as others have said, these old mysql_ functions are deprecated, so you should probably switch to mysqli or PDO if that is practical as well.
I think I am nearly there with this problem, but I can't quite see why my solution isn't working. I'm trying to pre-select an item from a php drop down list that uses a MySQL table. The drop down list populates as expected, but the pre-select doesn't work. My code:
echo $Trans1E; // Display Existing value
echo '<p>Trans1: ';
$q = "SELECT TR1_Name FROM sb_TR1 ORDER BY TR1_Name";
$r = #mysqli_query ($dbc, $q);
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
echo "<select name='Trans1' value=''>Trans1</option>"; // list box select command
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{//Array of records stored in $row
$selected='';
if($row[TR1_Name]==$Trans1E) //determine if the row value is the same as the Existing
{//if it it then mark as selected
echo "<option value=$row[TR1_Name] selected='selected'>$row[TR1_Name]</option>";
}
else
{// if it is not then just inlcude it in the drop down list
echo "<option value=$row[TR1_Name] >$row[TR1_Name]</option>";
}
}
echo "</select>";// Closing of list box
echo '
</p>';
I've seen similar things on this forum for drop down lists which I've incorporated in my code or at least experimented with but without success, I suspect an error in: if($row[TR1_Name]==$Trans1E), but have run out of thinsg to try.
It seems I was closer to the correct code than I thought, I resolved the problem by adding the line:
$Trans1E=mysqli_real_escape_string($dbc, trim(htmlentities($Trans1E)));
before the code.
Despite displaying the content of Trans1E, it seems there was an unwanted element in it, which I hadn't spotted so I thought the code further down was at fault.
Thanks for you comments. Hope this helps others.
I'm trying to create a select box full of options based on data from an array that is built from an sql query, I've tried researching where I'm going wrong but the closest I've managed to get is echoing alot of empty option boxes my code is as follows:
$result = mysqli_query($cons,"SELECT user_name FROM users");
while($row = mysqli_fetch_array($result)){
foreach($row as $key => $value){
echo '<option value="'.$value.'"</option>';}}
If anybody could point me in the right direction that would be great.
EDIT: I can't believe it was something as simple as that! Thank you very much.
On a separate note It's actually giving all of the data twice.
I'll mark as correct answer as soon as I can.
echo '<option value="'.$value.'">'.$value.'</option>';
or
echo "<option value=\"{$value}\">{$value}</option>";
You never closed the opening option tag. If you view your source you'll see it's wonky.
BTW, You don't need the value="" part if you're using the same value in the text of the option.
echo "<option>{$value}</option>";
Entire code as I would do it
$result = mysqli_query($cons,"SELECT user_id, user_name FROM users");
while($row = mysqli_fetch_assoc($result)){
echo "<option value=\"{$row['user_id']}\">{$row['user_name']}</option>";
}
i have a problem:
if(isset($_POST['send'])){
$id = mysql_real_escape_string($_POST['id']);
$query = mysql_query("select * from somewhere where id='$id'");
$row = mysql_fetch_array( $query );
if(!mysql_num_rows($query)==1){
echo('error');
}
}
After this i have this echo from db:
<input type="text" name="up_name" value="<?php echo $row['name'];?>" id="up_name"/>
and this echo:
<select>
<?php
$up_id=$_POST['up_id'];
$sqlDateUser=mysql_query("SELECT `something` from `somewhere` where id='".$id."'");
$res=mysql_fetch_assoc($sqlDateUser);
$somethig_selected=$res['something'];
$something=mysql_query("SELECT `den` FROM `jud`");
while($row = mysql_fetch_row( $something)){
$selected=($row[0]==$somethig_selected)?'selected':'';
echo "<option value='".$row[0]."' ".$selected.">".$row[0]."</option>";
}
?>
</select>
In this order all it is ok but if i change it, first echo doesn't work. I need to display and others rows after these and i don't know what is the problem with the second echo. Can someone tell me what is the problem?
It may be a naming issue. For example, you have your second query assigning your values for mysql_fetch_row into a variable named $row.
That also is the same name you've given your first query, where you are assigning your mysql_fetch_array.
I would probably try naming them something different like $row_get_user_info and $row_get_den to differentiate them. Also it would make it easier if, when you moved your first echo line, that you also move the query that goes along with it. (Keep them together.)
In the PHP code below i want to select all the column values of a table and make them options of a select form. The result is that i dont get any options at all. Could someone help? Thanks
<?php
// ....
$userid=$_SESSION['userid'];
echo "<select>";
$sql = "SELECT * FROM users where userid='".$userid."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<option>" .$row['company']. "</option>";
}
echo "</select>";
mysqli_close();
?>
It may just be a typo but you have mixed mysql and mysqli functions, you have stated mysql_query and mysql_fetch_array, whereas you have closed it with mysqli_close, could be an issue depending on what you wrote above this code or indeed if this is not just a typo.
Other things to try would be try the query in your mysql client / phpmyadmin and see if it comes up with any results or errors.