Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
条件下从数据库读取php mysql_Php_Mysql - Fatal编程技术网

条件下从数据库读取php mysql

条件下从数据库读取php mysql,php,mysql,Php,Mysql,因此,如果满足条件,应用程序应从数据库中读取数据,并以以下格式打印在页面上: 下面是代码,我将在最后发布我的输出图像 lib.php: 好的,我已经解决了我的第一个问题: line studenti[“indeks”]=lib.php中的$indeks出现了问题,但仍然找不到第二个问题的答案wow!把这么多信息都扔了。cmon man,通过只显示要点来简化我们解码wrk的工作..我也解决了第二个问题,尽管我不确定这是否是最佳解决方案:在函数vratiStudenta()中,第二个问题:我在“st

因此,如果满足条件,应用程序应从数据库中读取数据,并以以下格式打印在页面上: 下面是代码,我将在最后发布我的输出图像 lib.php:


好的,我已经解决了我的第一个问题:

line studenti[“indeks”]=lib.php中的$indeks出现了问题,但仍然找不到第二个问题的答案

wow!把这么多信息都扔了。cmon man,通过只显示要点来简化我们解码wrk的工作..我也解决了第二个问题,尽管我不确定这是否是最佳解决方案:在函数vratiStudenta()中,第二个问题:我在“strana1.tpl”的第二个和第三个循环中缺少key=indeks为所有这些图片感到抱歉,我想让你更容易得到应该做的代码
    <?php
$db_name = "ispit_septembar";
$db_user = "root";
$db_host = "localhost";
$db_pass = "";

function vratiStudente($godina)
{
global $db_name,$db_user,$db_host,$db_pass;
$studenti = array();
$link = mysql_connect("$db_host","$db_user","$db_pass") or die ("Nije moguca konekcija");

mysql_select_db("$db_name",$link) or die ("Nepostojeca baza");
$sql = mysql_query("SELECT * from studenti where godina = '$godina'");
while ($row = mysql_fetch_array($sql) )
    {
    $indeks = $row["indeks"];
    $imeiprezime = $row["imeiprezime"];
    $godina = $row["godina"];
    $studenti["indeks"] = $indeks;
    $studenti[$indeks]["imeiprezime"] = $imeiprezime;
    $studenti[$indeks]["godina"] = $godina;

    }
return $studenti;
}
function daLiPostojiStudent($indeks)
{
    global $db_name,$db_user,$db_host,$db_pass;
    $link = mysql_connect("$db_host","$db_user","$db_pass") or die ("Nije moguca konekcija");
    mysql_select_db("$db_name","$link") or die ("Nepostojeca baza");
    $sql = mysql_query("SELECT * from studenti where indeks = '$indeks'");
    $check = mysql_num_rows($sql);
    if ($check >0 )
    {
    $postoji = "postoji";
    }
    else
    {
    $postoji = "ne postoji";
    }
    return $postoji;
}
function dodajStudenta($indeks,$ime,$godina)
{
    global $db_name,$db_user,$db_host,$db_pass;
    $link = mysql_connect("$db_host","$db_user","$db_pass") or die ("Nije moguca konekcija");
    mysql_select_db("$db_name","$link") or die ("Nepostojeca baza");
    $result = daLiPostojiStudent($indeks);
    if ($result == "postoji")
    {
    $postoji = "Student sa tim brojem indeksa postoji";
    return $postoji;
    }
    else
    {
    $sql = ("INSERT INTO studenti (indeks,imeprezime,godina)
            VALUES ('$indeks','$ime','$godina' ");
    mysql_query($sql);
    return 1;
    }
}
?>
<?php
include "lib.php";
include "Smarty/libs/Smarty.class.php";
$dodaj1 = "strana2.php?godina=1";
$dodaj2 = "strana2.php?godina=2";
$dodaj3 = "strana2.php?godina=3";


$studenti1 = vratiStudente(1);
echo ($studenti1["indeks"]);
$studenti2 = vratiStudente(2);
$studenti3 = vratiStudente(3);



$smarty= new Smarty();
$smarty->assign("dodaj1",$dodaj1);
$smarty->assign("dodaj2",$dodaj2);
$smarty->assign("dodaj3",$dodaj3);
$smarty->assign("studenti1",$studenti1);
$smarty->assign("studenti2",$studenti2);
$smarty->assign("studenti3",$studenti3);
$smarty->display("strana1.tpl");


?>
<html>
    <head>
    </head>
    <body>
    Godina: 1  <a href ={$dodaj1}>Dodaj</a> 
<hr>
<table>

<tr>
    <th>Indeks</th>
    <th>Ime i Prezime </th>
</tr>
{foreach name="studenti1loop" item=student key=indeks from=$studenti1}
<tr>
    <td> {$indeks} </td>
    <td> {$student.imeiprezime}</td>
    <td>{$student.godina}</td>
</tr>
{/foreach}
</table>
{* druga godina*}
Godina: 2  <a href ={$dodaj2}>Dodaj</a> 
<hr>
<table>

<tr>
    <th>Indeks</th>
    <th>Ime i Prezime </th>
</tr>
{foreach name="studenti2loop" item=student from =$studenti2}
<tr>
    <td> {$indeks} </td>
    <td> {$student.imeiprezime}</td>
    <td>{$student.godina}</td>
</tr>
{/foreach}
</table>
{* treca godina*}
Godina: 3  <a href ={$dodaj3}>Dodaj</a> 
<hr>
<table>

<tr>
    <th>Indeks</th>
    <th>Ime i Prezime </th>
</tr>
{foreach name="studenti2loop" item=student from=$studenti3}
<tr>
    <td> {$indeks} </td>
    <td> {$student.imeiprezime}</td>
    <td>{$student.godina}</td>
</tr>
{/foreach}
</table>

</body>